package com.kingdee.shr.customer.gtiit.handler;

import com.google.common.collect.Lists;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.ats.*;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.ats.web.handler.AtsOverTimeBillListHandler;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * description: AtsOverTimeBillListHandlerEx <br>
 * date: 2025/3/31 10:11 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class AtsOverTimeBillListHandlerEx extends AtsOverTimeBillListHandler {


    public void submitEffectAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException, BOSException {
        Context ctx = SHRContext.getInstance().getContext();
        String billIds = request.getParameter("billId");
        String[] billIsArr = billIds.split(",");
        HashMap res = new HashMap();
        boolean bool = false;
        StringBuilder msg = new StringBuilder();
        try {
            IAtsOverTimeBill iaAtsOverTimeBill = AtsOverTimeBillFactory.getLocalInstance(ctx);
            SelectorItemCollection selectorItemCollection = new SelectorItemCollection();
            selectorItemCollection.add("id");
            selectorItemCollection.add("entries.otDate");
            selectorItemCollection.add("entries.person.id");
            for(String id : billIsArr){
                AtsOverTimeBillInfo ats = iaAtsOverTimeBill.getAtsOverTimeBillInfo(new ObjectUuidPK(id),selectorItemCollection);
                msg.append(this.checkOnlyTakeOneBill(ctx,ats,billIsArr));

            }
            if (msg.length()<=0) {
                super.submitAction(request, response, modelMap);
            }else {
                int submitNum = billIsArr.length;
                res.put("submitNum", submitNum);
                res.put("submitSuccessNum", 0);
                JSONUtils.writeJson(response, res);
            }
        } catch (BOSException | EASBizException e) {
            e.printStackTrace();
        }
    }
    public String submitAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        String billIds = request.getParameter("billId");
        String[] billIsArr = billIds.split(",");
        HashMap res = new HashMap();
        res.put("alreadyInProcessQueueException", 0);
        boolean bool = false;
        StringBuilder msg = new StringBuilder();
        try {
            IAtsOverTimeBill iaAtsOverTimeBill = AtsOverTimeBillFactory.getLocalInstance(ctx);
            SelectorItemCollection selectorItemCollection = new SelectorItemCollection();
            selectorItemCollection.add("id");
            selectorItemCollection.add("entries.otDate");
            selectorItemCollection.add("entries.person");
            selectorItemCollection.add("entries.person.id");
            selectorItemCollection.add("entries.person.name");
            for(String id : billIsArr){
                AtsOverTimeBillInfo ats = iaAtsOverTimeBill.getAtsOverTimeBillInfo(new ObjectUuidPK(id),selectorItemCollection);
                msg.append(this.checkOnlyTakeOneBill(ctx,ats,billIsArr));

            }
            if (msg.length()<=0) {
                super.submitAction(request, response, modelMap);
            }else {
                int submitNum = billIsArr.length;
                res.put("submitNum", submitNum);
                res.put("submitSuccessNum", 0);
                JSONUtils.writeJson(response, res);
            }
        } catch (BOSException | EASBizException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 一位员工每个自然月只能提一张加班单校验
     * @param ctx
     * @param model
     */
    protected StringBuilder checkOnlyTakeOneBill(Context ctx, CoreBaseInfo model,String[] billIsArr) throws ShrWebBizException {
        boolean bool = true;
        StringBuilder msg = new StringBuilder();
        try {
            AtsOverTimeBillInfo billInfo = (AtsOverTimeBillInfo) model;
            AtsOverTimeBillEntryCollection entries = billInfo.getEntries();
            StringBuilder sql = new StringBuilder();
            sql.append(" select count(b.fid) lens,e.fpersonid from T_HR_ATS_OVERTIMEBILL b  ");
            sql.append(" left join T_HR_ATS_OVERTIMEBILLentry e on b.fid= e.fbillid ");
            sql.append(" where b.fbillstate != '4' ");
            sql.append(" and to_char(e.fotdate,'yyyy-MM-dd')>= ? and to_char(e.fotdate,'yyyy-MM-dd')<= ? ");
            List<String> list = Lists.newArrayList();
            list.add("");
            list.add("");
            if(null!=billIsArr) {
                sql.append(" or b.fid in(''");
                for (int i = 0; i < billIsArr.length; i++) {
                    sql.append(",?");
                    list.add(billIsArr[i]);
                }
                sql.append(" ) ");
            }
            sql.append(" group by e.fpersonid ");
            System.out.println("checkOnlyTakeOneBill:"+sql.toString());
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            BigDecimal one = BigDecimal.ONE;
            for (int i = 0; i < entries.size(); i++) {
                AtsOverTimeBillEntryInfo ats = entries.get(i);
                Date otdate = ats.getOtDate();
                PersonInfo personInfo = ats.getPerson();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(otdate);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                String fotdate1 = sf.format(calendar.getTime());
                calendar.add(Calendar.MONTH, 1);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                String fotdate2 = sf.format(calendar.getTime());
                list.set(0,fotdate1);
                list.set(1,fotdate2);
                System.out.println("checkOnlyTakeOneBill_values:"+list.toString());
                IRowSet rs = DbUtil.executeQuery(ctx, sql.toString(), list.toArray());
                while (rs.next()){
                    String fpersonid = rs.getString("fpersonid");
                    BigDecimal lens = rs.getBigDecimal("lens");
                    System.out.println("checkOnlyTakeOneBill_fpersonid:"+fpersonid);
                    System.out.println("checkOnlyTakeOneBill_personid:"+personInfo.getId().toString());
                    System.out.println("checkOnlyTakeOneBill_lens:"+lens.toString());
                    System.out.println("checkOnlyTakeOneBill_one:"+one.toString());
                    if(fpersonid.equals(personInfo.getId().toString()) && one.compareTo(lens) <= 0){
                        bool=false;
                        msg.append("Each employee can apply for overtime only once per natural month.Employee ("+personInfo.getName()+") has already applied.\r\n");
                        System.out.println("checkOnlyTakeOneBill_msg:"+msg.toString());
                        break;
                    }
                }
            }
        }catch (BOSException | SQLException e){
            e.printStackTrace();
            bool=false;
        }finally {

        }
        return msg;
    }
}
