package com.liang.scheduled;

import cn.hutool.core.collection.CollUtil;
import com.liang.common.utils.SpringUtil;
import com.liang.dao.StaBudgetapprovalDao;
import com.liang.entity.PrjProject;
import com.liang.entity.StaBudgetapproval;
import com.liang.service.PrjProjectService;
import com.liang.service.StaBudgetapprovalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@Component
@EnableScheduling // 开启定时任务功能
@Slf4j
@RestController
@RequestMapping("ScheduledEvent")
public class ScheduledEvent {

/**
     * 更新审批结束的费用申请的科研编号
     *//*

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void updateStaBudgetApprovalBh() {
        log.info("============================开始更新费用申请的科研编号======================");
        log.info("===================================================================");

        List<StaBudgetapproval> staBudgetapprovals = staBudgetapprovalDao.listSPJS();
        if (CollUtil.isNotEmpty(staBudgetapprovals)) {
            log.info("============================待更新费用申请:{}======================", staBudgetapprovals);
            //设置编号
            staBudgetapprovals.forEach(item -> {
                DateFormat dateFormat = new SimpleDateFormat("yyyy");
                PrjProject project = prjProjectService.queryById(item.getXmid());
                if (project != null && project.getXmlxmc().indexOf("政府") > 0) {
                    String partBh = "结转类(" + dateFormat.format(new Date()) + ")";
                    if (BH.get(partBh) != null) {
                        BH.put(partBh, (BH.get(partBh) + 1));
                        item.setBh(partBh + (BH.get(partBh)));
                    } else {
                        BH.put(partBh,1001);
                        item.setBh(partBh + 1001);
                    }
                } else {
                    if (item.getSqje() < 1000000) {
                        String partBh = "科研类(" + dateFormat.format(new Date()) + ")";
                        if (BH.get(partBh) != null) {
                            BH.put(partBh, (BH.get(partBh) + 1));
                            item.setBh(partBh + (BH.get(partBh)));
                        } else {
                            BH.put(partBh,1001);
                            item.setBh(partBh + 1001);
                        }
                    } else {
                        String partBh = "大额科研类(" + dateFormat.format(new Date()) + ")";
                        if (BH.get(partBh) != null) {
                            BH.put(partBh, (BH.get(partBh) + 1));
                            item.setBh(partBh + (BH.get(partBh)));
                        } else {
                            BH.put(partBh,1001);
                            item.setBh(partBh + 1001);
                        }
                    }
                }
            });
            //更新编号
            try {
                staBudgetapprovalDao.updateBatchBh(staBudgetapprovals);
            } catch (Exception e) {
                log.error("ERROR:更新审批结束的费用申请的科研编号时出错,错误信息:" + e.getMessage());
            }
        } else {
            log.info("============================暂无科研编号需要更新======================");
        }

    }

    static Map<String, Integer> BH = new HashMap<>();

    static {
        DateFormat dateFormat = new SimpleDateFormat("yyyy");
        String partBhZjl = "结转类(" + dateFormat.format(new Date()) + ")";
        String partBhKyl = "科研类(" + dateFormat.format(new Date()) + ")";
        String partBhDekyl = "大额科研类(" + dateFormat.format(new Date()) + ")";
        List<String> bhName = new ArrayList<>();
        Collections.addAll(bhName, partBhZjl, partBhKyl, partBhDekyl);
        bhName.forEach(ScheduledEvent::getApproveRecordBH);

    }

    private static void getApproveRecordBH(String partBh) {
        StaBudgetapprovalService staBudgetapprovalService = SpringUtil.getBean("staBudgetapprovalService");
        List<StaBudgetapproval> bhList = staBudgetapprovalService.queryByBH(partBh);
        List<Integer> bh_List = new ArrayList();
        if (bhList != null && bhList.size() > 0) {
            for (int i = 0; i < bhList.size(); i++) {
                int _tmpBh = Integer.parseInt(bhList.get(i).getBh().substring(bhList.get(i).getBh().length() - 4));
                bh_List.add(_tmpBh);
            }
            if (bh_List.size() > 0) {
                try {
                    Integer max = Collections.max(bh_List);
                    BH.put(partBh, max);
                } catch (Exception er) {
                    BH.put(partBh, 1001);
                }
            } else {
                BH.put(partBh, 1001);
            }
        } else {
            BH.put(partBh, 1001);
        }
    }
*/

}
