package com.sifang.modules.flowable.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowable.dao.ItemManageDao;
import com.sifang.modules.flowable.entity.ItemManage;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.flowable.vo.DealItemVo;
import com.sifang.modules.flowpath.service.SubpackageStandardPriceApplyService;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.common.utils.StringUtil;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.AuthUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ItemManageService extends BaseService {

    @Autowired
    private ItemManageDao itemManageDao;

    @Autowired
    private SubpackageStandardPriceApplyService standardPriceApplyService;

    public void lcWaiteDealItemList(LayuiPage<Record> page, Map<String, String> map, Operator operator, HttpServletRequest request){
        //判断用户是否有电站各个环节审批的操作权限
        List<String> authEntry=new ArrayList<>();
        List<String> changeEntry=new ArrayList<>();
        checkAuth(authEntry,changeEntry,request);
        checkAuth2(changeEntry,authEntry,request);
        List<Record> list= itemManageDao.LcWaiteDealItemList(page,map,operator,authEntry,changeEntry);
//        List<Record> list= itemManageDao.waiteDealItemList(page,map,operator,authEntry,changeEntry);
        String currentTime=DateUtils.getDateTime();
        for(Record record:list){
            if(record.get("createTime")!=null){
                Long betMin= DateUtils.getTwoTimeBetMin(record.getStr("createTime"),currentTime);
                BigDecimal betHour=getBetHour(betMin);
                record.set("dueTime",betHour);
            }
            if ("06".equals(record.getStr("formKey"))){
                String applicationId = record.getStr("application_id");
                List<Record> items=standardPriceApplyService.getApplyItems(applicationId);
                for (Record item : items) {
                    String name = item.getStr("name");
                    record.set("planName",name);
                }
            }
        }
        page.setData(list);
    }

    //已处理列表数据 (流程事项)
    public void lcEndDealItemList(LayuiPage<Record> page, Map<String, String> map,Operator operator){
        List<Record> list= itemManageDao.lcEndDealItemList(page,map,operator);
        for(Record record:list){
            if(record.get("handleTime")!=null){
                if(record.get("taskCreateTime")!=null){
                    Long betMin=DateUtils.getTwoTimeBetMin(record.getStr("taskCreateTime"),record.getStr("handleTime"));
                    BigDecimal betHour=getBetHour(betMin);
                    record.set("dueTime",betHour);
                }else if(record.get("createTime")!=null){
                    Long betMin=DateUtils.getTwoTimeBetMin(record.getStr("createTime"),record.getStr("handleTime"));
                    BigDecimal betHour=getBetHour(betMin);
                    record.set("dueTime",betHour);
                }

            }
        }
        page.setData(list);
    }

    //待处理 列表数据
    public void projWaiteDealItemList(LayuiPage<Record> page, Map<String, String> map, Operator operator, HttpServletRequest request, String queryType){
        //判断用户是否有电站各个环节审批的操作权限
        List<String> authEntry=new ArrayList<>();
        List<String> changeEntry=new ArrayList<>();
        checkAuth(authEntry,changeEntry,request);
        List<Record> list= itemManageDao.projWaiteDealItemListBy(page,map,operator,authEntry,changeEntry, queryType);
//        String currentTime=DateUtils.getDateTime();
//        for(Record record:list){
//            if(record.get("createTime")!=null){
//                Long betMin= DateUtils.getTwoTimeBetMin(record.getStr("createTime"),currentTime);
//                BigDecimal betHour=getBetHour(betMin);
//                record.set("dueTime",betHour);
//            }
//        }
        page.setData(list);
    }

    //已处理列表数据 (项目事项)
    public void projEndDealItemList(LayuiPage<Record> page, Map<String, String> map,Operator operator){
        List<Record> list= itemManageDao.projEndDealItemList(page,map,operator);
//        for(Record record:list){
//            if(record.get("handleTime")!=null){
//                if(record.get("taskCreateTime")!=null){
//                    Long betMin=DateUtils.getTwoTimeBetMin(record.getStr("taskCreateTime"),record.getStr("handleTime"));
//                    BigDecimal betHour=getBetHour(betMin);
//                    record.set("dueTime",betHour);
//                }else if(record.get("createTime")!=null){
//                    Long betMin=DateUtils.getTwoTimeBetMin(record.getStr("createTime"),record.getStr("handleTime"));
//                    BigDecimal betHour=getBetHour(betMin);
//                    record.set("dueTime",betHour);
//                }
//
//            }
//        }
        page.setData(list);
    }

    //待处理 列表数据
    public void waiteDealItemList(LayuiPage<Record> page, Map<String, String> map, Operator operator, HttpServletRequest request){
        //判断用户是否有电站各个环节审批的操作权限
        List<String> authEntry=new ArrayList<>();
        List<String> changeEntry=new ArrayList<>();
        checkAuth(authEntry,changeEntry,request);
       List<Record> list= itemManageDao.waiteDealItemList(page,map,operator,authEntry,changeEntry);
       String currentTime=DateUtils.getDateTime();
       for(Record record:list){
           if(record.get("createTime")!=null){
               Long betMin= DateUtils.getTwoTimeBetMin(record.getStr("createTime"),currentTime);
               BigDecimal betHour=getBetHour(betMin);
               record.set("dueTime",betHour);
           }
       }
       page.setData(list);
    }

    //已处理列表数据
    public BigDecimal getBetHour(Long betMin){
        BigDecimal betHour=BigDecimal.valueOf(betMin).divide(new BigDecimal(60),1, BigDecimal.ROUND_HALF_UP);
        BigDecimal zsHour=BigDecimal.valueOf(betMin).divide(new BigDecimal(60),0, BigDecimal.ROUND_DOWN).add(new BigDecimal("0.5"));
        logger.info(betHour+"***********"+zsHour+"*******"+betMin);

        if(betHour.compareTo(zsHour)>0){
           betHour=zsHour;
        }else if(betHour.compareTo(zsHour)<0){
             betHour=BigDecimal.valueOf(betMin).divide(new BigDecimal(60),0, BigDecimal.ROUND_HALF_UP);
        }
      return betHour;
    }

    public void endDealItemList(LayuiPage<Record> page, Map<String, String> map,Operator operator){
        List<Record> list= itemManageDao.endDealItemList(page,map,operator);
        for(Record record:list){
            if(record.get("handleTime")!=null){
                if(record.get("taskCreateTime")!=null){
                    Long betMin=DateUtils.getTwoTimeBetMin(record.getStr("taskCreateTime"),record.getStr("handleTime"));
                    BigDecimal betHour=getBetHour(betMin);
                    record.set("dueTime",betHour);
                }else if(record.get("createTime")!=null){
                    Long betMin=DateUtils.getTwoTimeBetMin(record.getStr("createTime"),record.getStr("handleTime"));
                    BigDecimal betHour=getBetHour(betMin);
                    record.set("dueTime",betHour);
                }

            }
        }
        page.setData(list);
    }

    public List<DealItemVo> getWaitDealListForHomePage(LayuiPage<Record> page, Map<String, String> map, Operator operator, HttpServletRequest request) {
        //判断用户是否有电站各个环节审批的操作权限
        List<String> authEntry=new ArrayList<>();
        List<String> changeEntry=new ArrayList<>();
        checkAuth(authEntry,changeEntry,request);
        List<Record> list = itemManageDao.waiteDealItemList(page, map, operator, authEntry,changeEntry);
        List<DealItemVo> dealItemVoList= new ArrayList<>();
        for (Record r : list) {
            DealItemVo vo = new DealItemVo();
            vo.setId(r.getStr("application_id"));
            if(StringUtil.equalsAny(r.getStr("formKey"),"95","96","97","98","99","100")){
                vo.setId(r.getStr("process_instance_id"));
            }
            vo.setFormkey(r.getStr("formKey"));
            vo.setType(changeType(r.getStr("formKey")));
            vo.setName(r.getStr("name"));
            vo.setCreateName(r.getStr("createName"));
            dealItemVoList.add(vo);
        }
        return dealItemVoList;
    }
    
    private String changeType(String formKey) {
        switch (formKey) {
            case "01":
                return "代理商准入申请";
            case "02":
                return "供应商准入申请";
            case "03":
                return "保证金调整申请";
            case "04":
                return "授信额调整申请";
            case "05":
                return "质保金调整申请";
            case "06":
                return "分包标准价申请";
            case "07":
                return "结算额调整申请";
            case "08":
                return "赊销下单申请";
            case "09":
                return "采购申请";
            case "10":
                return "盘点单申请";
            case "11":
                return "调拨单申请";
            case "12":
                return "请款申请";
            case "13":
                return "仓库申请";
            case "14":
                return "零售价系数申请";
            case "15":
                return "备货申请";
            case "16":
                return "支架账户申请";
            case "17":
                return "现金下单申请";
            case "18":
                return "保证金额度申请";
            case "91":
                return "设计审核";
            case "92":
                return "档案审核";
            case "93":
                return "安装验收";
            case "94":
                return "并网验收";
            case "95":
                return "勘测信息变更";
            case "96":
                return "完工信息变更";
            case "97":
                return "设计信息变更";
            case "98":
                return "客户信息变更";
            case "99":
                return "物料信息变更";
            case "100":
                return "并网信息变更";
            default:
                return "未知";
        }
    }

    public void checkAuth(List<String> authEntry,List<String> changeEntry,HttpServletRequest request){
        //设计审核
        String sjsh= AuthUtils.checkPagePermission(request,"XMGL_XMGL_DSHXX_SH_B");
        if("0".equals(sjsh)){
            authEntry.add(FormUtils.ENTRY_STATUS_SJSH);
        }
        //安装验收
        String azys=AuthUtils.checkPagePermission(request,"XMGL_AZYS_AZYS_YS");
        if("0".equals(azys)){
            authEntry.add(FormUtils.ENTRY_STATUS_AZYS);
        }
        //并网验收
        String bwys=AuthUtils.checkPagePermission(request,"XMGL_BWYS_BWYS_YS");
        if("0".equals(bwys)){
            authEntry.add(FormUtils.ENTRY_STATUS_BWYS);
        }
        //档案审核
        String dash=AuthUtils.checkPagePermission(request,"XMGL_XMGL_DSHXX_DASH_B");
        if("0".equals(dash)){
            authEntry.add(FormUtils.ENTRY_STATUS_DASH);
        }
        //客户信息变更
        String khxxbg=AuthUtils.checkPagePermission(request,"DZBG_KHXXBG_DSH_SH_B");
        if("0".equals(khxxbg)){
            changeEntry.add(FormUtils.ENTRY_CHANGE_KHXXBG);
        }
        //完工信息变更
        String wgxxbg=AuthUtils.checkPagePermission(request,"DZBG_WGXXBG_DSH_T");
        if("0".equals(wgxxbg)){
            changeEntry.add(FormUtils.ENTRY_CHANGE_WGBG);
        }
        //设计变更
        String sjbg=AuthUtils.checkPagePermission(request,"DZBG_SJBG_DSH_AUDIT_B");
        if("0".equals(sjbg)){
            changeEntry.add(FormUtils.ENTRY_CHANGE_SJBG);
        }
        //物料信息变更
        String wlxxbg=AuthUtils.checkPagePermission(request,"DZBG_WLXXBG_DSH_SH_B");
        if("0".equals(wlxxbg)){
            changeEntry.add(FormUtils.ENTRY_CHANGE_WLXXBG);
        }
        //勘测信息变更
        String kcxxbg=AuthUtils.checkPagePermission(request,"DZBG_KCXXBG_DSH_T");
        if("0".equals(kcxxbg)){
            changeEntry.add(FormUtils.ENTRY_CHANGE_KCBG);
        }
        //并网信息变更
        String bwxxbg=AuthUtils.checkPagePermission(request,"DZBG_BWXXBG_DSH_SH_B");
        if("0".equals(bwxxbg)){
            changeEntry.add(FormUtils.ENTRY_CHANGE_BWXXBG);
        }
    }

    public void checkAuth2(List<String> zrEntry,List<String> authEntry,HttpServletRequest request){
        //代理商准入申请
        String sjsh= AuthUtils.checkPagePermission(request,"DSPYW_DLSZRSQ_DLSZRSQ");
        if("0".equals(sjsh)){
            zrEntry.add(FormUtils.AGENT_ADMISSION_APPLICATION);
        }
        //供应商准入申请
        String azys=AuthUtils.checkPagePermission(request,"SYS_DSPYW_GYSSQ_T");
        if("0".equals(azys)){
            zrEntry.add(FormUtils.SUPPLIER_ADMISSION_APPLICATION);
        }
        //保证金调整申请
        String bwys=AuthUtils.checkPagePermission(request,"DSPYE_BZJTZSQ_BZJTZSQ_T");
        if("0".equals(bwys)){
            zrEntry.add(FormUtils.MARGIN_ADJUSTMENT_APPLICATION);
        }
        //授信额调整申请
        String dash=AuthUtils.checkPagePermission(request,"DSPYE_PHETZSQ_PHETZSQ_T");
        if("0".equals(dash)){
            zrEntry.add(FormUtils.DISTRIBUTION_ADJUSTMENT_APPLICATION);
        }
        //质保金调整申请
        String khxxbg=AuthUtils.checkPagePermission(request,"DSPYE_ZBJTZSQ_ZBJTZSQ_T");
        if("0".equals(khxxbg)){
            zrEntry.add(FormUtils.WITHDRAWAL_WARRANTY_APPLICATION);
        }
        //分包标准价申请
//        String wgxxbg=AuthUtils.checkPagePermission(request,"DZBG_WGXXBG_DSH_T");
//        if("0".equals(wgxxbg)){
//            changeEntry.add(FormUtils.SUBCONTRACTING_STANDARD_PRICE_APPLICATION);
//        }
        //结算额调整申请
        String sjbg=AuthUtils.checkPagePermission(request,"DSPYE_JSETZSQ_JSETZSQ_T");
        if("0".equals(sjbg)){
            zrEntry.add(FormUtils.SETTLE_AMOUNT_ADJUST_APPLICATION);
        }
        //盘点单申请Inventory
//        String wlxxbg=AuthUtils.checkPagePermission(request,"DZBG_WLXXBG_DSH_SH_B");
//        if("0".equals(wlxxbg)){
//            changeEntry.add(FormUtils.ENTRY_CHANGE_WLXXBG);
//        }
        //调拨单申请
        String kcxxbg=AuthUtils.checkPagePermission(request,"CKGL_DBDGL_DBDGL_T");
        if("0".equals(kcxxbg)){
            zrEntry.add(FormUtils.TRANSFER_ORDER_APPLICATION);
        }
        //赊销申请
        String sxxd=AuthUtils.checkPagePermission(request,"JXSDDGL_DSPDD_DSPDD_T");
        if("0".equals(sxxd)){
            zrEntry.add(FormUtils.SUPPLY_ORDER_APPLICATION);
        }
        //采购申请
        String cgsq=AuthUtils.checkPagePermission(request,"GYSDD_CGSP_CGSP_T");
        if("0".equals(cgsq)){
            zrEntry.add(FormUtils.PURCHASE_ORDER_APPLICATION);
        }
        //请款申请
        String qksq=AuthUtils.checkPagePermission(request,"DSPYW_TXSQ_TXSQ_T");
        if("0".equals(qksq)){
            zrEntry.add(FormUtils.CASH_OUT_APPLY);
        }
        //零售价系数申请
        String lsjg=AuthUtils.checkPagePermission(request,"DSPYE_LSJXSSQ_LSJXSSQ_T");
        if("0".equals(lsjg)){
            zrEntry.add(FormUtils.PRICE_COEFFICIENT_APPLY);
        }
        //仓库申请
        String cksq=AuthUtils.checkPagePermission(request,"DSPYW_CKSQ_CKSQ_T");
        if("0".equals(cksq)){
            zrEntry.add(FormUtils.WAREHOUSE_APPLY);
        }
        //备货申请
//        String kcxxbg=AuthUtils.checkPagePermission(request,"DZBG_KCXXBG_DSH_T");
//        if("0".equals(kcxxbg)){
//            changeEntry.add(FormUtils.ENTRY_CHANGE_KCBG);
//        }
        //保证金额度申请
        String bzjed=AuthUtils.checkPagePermission(request,"DSPYE_BZJTZSQ_BZJTZSQ_T");
        if("0".equals(bzjed)){
            zrEntry.add(FormUtils.BOND_LIMIT_APPLY);
        }
        //支架账户申请
        String zjzhsq=AuthUtils.checkPagePermission(request,"ZHGL_ZJZH_ZJZH_T");
        if("0".equals(zjzhsq)){
            zrEntry.add(FormUtils.BRACKET_ACCOUNT_APPLY);
        }
        //现金下单申请
        String xjxd=AuthUtils.checkPagePermission(request,"JXSDDGL_DSPDD_ZJDSPDD_T");
        if("0".equals(xjxd)){
            zrEntry.add(FormUtils.BRACKET_ORDER_APPLY);
        }

    }

    public ItemManage getById(String itemId) {
        return itemManageDao.get(itemId);
    }

    public List<Record> findByIds(List<String> itemIds) {
        return itemManageDao.findByIds(itemIds);
    }

    public List<Record> getExamineLogList(Map<String, String> map) {
        return itemManageDao.getExamineLogList(map);
    }

}
