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.CkPublicBenefitsMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkPublicBenefitsZationMapper;
import cn.com.countrygarden.bi.main.model.ck.CkPublicBenefits;
import cn.com.countrygarden.bi.main.model.ck.CkPublicBenefitsZation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.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.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Configuration
@EnableScheduling
public class CkPublicBenefitsZationSchedu {

    CkPublicBenefitsZationMapper ckPublicBenefitsZationMapper= ApplicationContextRegister.getApplicationContext().getBean(CkPublicBenefitsZationMapper.class);

    CkPublicBenefitsMapper ckPublicBenefitsMapper=ApplicationContextRegister.getApplicationContext().getBean(CkPublicBenefitsMapper.class);


    @Scheduled(cron = "1 2 2 1 1 ?")
    public void addOrEdit() throws ParseException {
        log.info("公共收益定时器执行开始---------------------------");
       System.out.println("公共收益定时器执行开始---------------------------");

       CkPublicBenefitsZation ck=new CkPublicBenefitsZation();
       ck.setStatus(1);
       List<CkPublicBenefitsZation> CkPublicList=ckPublicBenefitsZationMapper.select(ck);
       if(CollectionUtils.isNotEmpty(CkPublicList)){
           Calendar cal = Calendar.getInstance();
           int year = cal.get(Calendar.YEAR);

           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
           for (int i = 0; i <CkPublicList.size() ; i++) {
               List<CkPublicBenefits> addlist=new ArrayList<>();
               for (int j = 1; j <=12 ; j++) {
                   System.out.println(j);
                   CkPublicBenefits publicBenefits=new CkPublicBenefits();
                   publicBenefits.setOrgCode(CkPublicList.get(i).getOrgCode());
                   publicBenefits.setOrgName(CkPublicList.get(i).getOrgName());
                   publicBenefits.setCommCode(CkPublicList.get(i).getCommCode());
                   publicBenefits.setCommName(CkPublicList.get(i).getCommName());
                   publicBenefits.setYear(year);
                   publicBenefits.setMonth(j);
                   publicBenefits.setStatus(1);

                   CkPublicBenefits benefits=ckPublicBenefitsMapper.selectOne(publicBenefits);
                   if(benefits!=null){
                       continue;
                   }
                   publicBenefits.setIsAuto(1);
                   cal.setTime(sdf.parse(publicBenefits.getYear()+"-"+publicBenefits.getMonth()));
                   publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()),1,1).lengthOfYear());
                   publicBenefits.setMonthday(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                   publicBenefits.setUct(CkPublicList.get(i).getUct());
                   publicBenefits.setEdutax(CkPublicList.get(i).getEdutax());
                   publicBenefits.setLedtax(CkPublicList.get(i).getLedtax());
                   publicBenefits.setOther(CkPublicList.get(i).getOther());
                   publicBenefits.setBacirate(CkPublicList.get(i).getBacirate());
                   publicBenefits.setStampduty(CkPublicList.get(i).getStampduty());
                   publicBenefits.setCreateTime(new Date());
                   publicBenefits.setUpdateTime(new Date());
                   addlist.add(publicBenefits);
               }
               if(addlist.size()!=0){
                   ckPublicBenefitsMapper.insertList(addlist);
               }
           }

       }





        log.info("公共收益定时器执行完毕---------------------");
       System.out.println("公共收益定时器执行完毕---------------------------");

   }




}
