package cn.com.countrygarden.bi.main.service.ck;

import cn.com.countrygarden.bi.main.constant.wcx.ApplicationContextRegister;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerFileidRoleMapper;
import cn.com.countrygarden.bi.main.dao.ck.ksh.CkKshBasicMapper;
import cn.com.countrygarden.bi.main.dao.ck.ksh.CkKshExpandMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysOrganMapper;
import cn.com.countrygarden.bi.main.dto.advise.ProjectListVO;
import cn.com.countrygarden.bi.main.model.ck.CkCleanerFileidRole;
import cn.com.countrygarden.bi.main.model.ck.ksh.CkKshBasic;
import cn.com.countrygarden.bi.main.model.ck.ksh.CkKshExpand;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Configuration
@EnableScheduling
public class CkKshExpandShedu {

    CkKshBasicMapper ckKshBasicMapper= ApplicationContextRegister.getApplicationContext().getBean(CkKshBasicMapper.class);

    CkKshExpandMapper ckKshExpandMapper= ApplicationContextRegister.getApplicationContext().getBean(CkKshExpandMapper.class);

    TbSysOrganMapper tbSysOrganMapper= ApplicationContextRegister.getApplicationContext().getBean(TbSysOrganMapper.class);

    CkCleanerFileidRoleMapper ckCleanerFileidRoleMapper= ApplicationContextRegister.getApplicationContext().getBean(CkCleanerFileidRoleMapper.class);

    @Scheduled(cron = "2 1 2 25 * ?")
    public void addCkKsh() throws ParseException {

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfoAdddsq(null);

        List<ProjectListVO> prtLinew=new ArrayList<>();

        for (int i = 0; i <projectList.size() ; i++) {
            if(projectList.get(i).getOrganName().indexOf("区域")!=-1){
                prtLinew.add(projectList.get(i));
            }
        }


        List<ProjectListVO> prtLi=new ArrayList<>();
        CkCleanerFileidRole ckCleanerFileidRole=new CkCleanerFileidRole();
        ckCleanerFileidRole.setRoleCode("ck_ksh_admin");
        ckCleanerFileidRole.setStatus(1);
        List<CkCleanerFileidRole> ckCleanerFileidRoles=ckCleanerFileidRoleMapper.select(ckCleanerFileidRole);
        for (int i = 0; i <prtLinew.size() ; i++) {

            ProjectListVO proj = new ProjectListVO();
            if (StringUtil.isBlank(prtLinew.get(i).getActualEnterTime()) ||
                    StringUtil.isBlank(prtLinew.get(i).getActualEnterTime())) {
                proj = prtLinew.get(i);
            } else {
                Date dateCp = simpleDateFormat.parse(prtLinew.get(i).getContractProperty());
                Date dateAe = simpleDateFormat.parse(prtLinew.get(i).getActualEnterTime());

                long tsCp = dateCp.getTime();
                long tsAe = dateAe.getTime();
                String actuTime = prtLinew.get(i).getActualEnterTime();

                String[] srig = actuTime.split("-");
                Integer nian = Integer.valueOf(srig[0]);
                Integer yue = Integer.valueOf(srig[1]);
                if(nian.equals(year)){
                    if (yue.equals(month)) {
                        if (tsAe < tsCp) {
                            proj = prtLinew.get(i);
                        }
                    } else {
                        proj = prtLinew.get(i);
                    }
                }else {
                    proj=prtLinew.get(i);
                }

            }
            prtLi.add(proj);
        }

        for (int a = 0; a <prtLi.size() ; a++) {
            CkKshBasic ckCleerBas = new CkKshBasic();
            ckCleerBas.setOrgCode(prtLi.get(a).getOrganCode());
            ckCleerBas.setOrgName(prtLi.get(a).getOrganName());
            ckCleerBas.setCommCode(prtLi.get(a).getCommId());
            ckCleerBas.setCommName(prtLi.get(a).getCommName());
            ckCleerBas.setYear(String.valueOf(year));
            ckCleerBas.setMonth(String.valueOf(month));
            ckCleerBas.setStatus(1);
            List<CkKshBasic> ckKshBasicslist=ckKshBasicMapper.select(ckCleerBas);
            if(CollectionUtils.isEmpty(ckKshBasicslist)){
                ckCleerBas.setIsDisplay(0);
                if(StringUtil.isNotBlank(prtLi.get(a).getActualEnterTime())){
                    ckCleerBas.setActualEnterTime(prtLi.get(a).getActualEnterTime().substring(0,10));
                }
                ckCleerBas.setContractProperty(prtLi.get(a).getContractProperty().substring(0,10));
                ckCleerBas.setIsFillin(0);
                ckCleerBas.setIsLocking(0);
                ckCleerBas.setIsUnlocking(0);
                ckCleerBas.setIsSubmit(0);
                ckCleerBas.setCreateTime(new Date());
                int index=ckKshBasicMapper.insert(ckCleerBas);
                if(index>0){
                    for (int j = 0; j <ckCleanerFileidRoles.size() ; j++) {
                        CkKshExpand ckKshExpand=new CkKshExpand();
                        ckKshExpand.setStatus(1);
                        ckKshExpand.setBasicId(ckCleerBas.getId());
                        ckKshExpand.setFieldCode(ckCleanerFileidRoles.get(j).getFieldCode());
                        ckKshExpand.setCreateTime(new Date());
                        ckKshExpand.setValue("");
                        ckKshExpandMapper.insert(ckKshExpand);
                    }
                }
            }
        }


    }

    @Scheduled(cron = "2 1 1 25 * ?")
    public void updateCkLoking(){
        CkKshBasic ckCleerBas = new CkKshBasic();
        ckCleerBas.setIsLocking(0);
        ckKshBasicMapper.updateByPrimaryLocking(ckCleerBas);
    }


    @Scheduled(cron = "1 1 1 6 * ?")
    public void updateCkKsh(){
        CkKshBasic ckCleerBas = new CkKshBasic();
        ckCleerBas.setIsLocking(1);
        ckCleerBas.setIsUnlocking(0);
        ckKshBasicMapper.updateByPrimaryLocking(ckCleerBas);
    }





}
