package com.qy.worksheetsystem.service.impl;

import cn.hutool.json.JSONUtil;
import com.qy.worksheetsystem.model.mapper.ProjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * description: ProjectServiceImpl <br>
 * date: 2022/3/12 10:17 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
@Slf4j
@Service
public class ProjectServiceImpl {
    @Resource
    private ProjectMapper mapper;
    @Value("${HR.OrgNumber}")
    private String OrgNumber;


    public Map<String,String> getProjectListSingle(String number,String id){
        Map<String,String> temp = mapper.getProjectListSingle(number,id);
        Map<String,String> map = new HashMap<>();
        map.put("id",temp.get("orgId"));
        map.put("name",temp.get("orgName"));
        String begin = temp.get("beginDate");
        String end = temp.get("endDate");
        String beginarr = begin.substring(0,7);
        String endarr = end.substring(0,7);

        String monthCircle ="";
        //现在日期
        LocalDate nowLocal = LocalDate.now();

        int num = nowLocal.getMonth().length(nowLocal.isLeapYear());
        String nowarr=nowLocal.format(DateTimeFormatter.ISO_LOCAL_DATE).substring(0,7);
        LocalDate beginLocal = LocalDate.parse(begin, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDate endLocal = LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE);
        //判断结束时间是否等于现在时间
        // 不同年月，并且现在时间小于结束时间
        if((!nowarr.equals(endarr))&&nowLocal.compareTo(endLocal)<0) {
            if (nowarr.equals(beginarr)) {
                if (nowLocal.compareTo(beginLocal) >= 0) {
                    monthCircle = nowLocal.getDayOfMonth() + "-" + num;
                } else {
                    monthCircle = beginLocal.getDayOfMonth() + "-" + num;
                }
            } else {
                if(nowLocal.compareTo(beginLocal) >= 0){
                    monthCircle = nowLocal.getDayOfMonth() + "-" + num;
                }else {
                    num = beginLocal.getMonth().length(beginLocal.isLeapYear());
                    monthCircle = beginLocal.getDayOfMonth() + "-" + num;
                }
            }
            //同年月，并且现在时间小于结束时间
        }else if(nowarr.equals(endarr)&&nowLocal.compareTo(endLocal)<=0){
            if (nowarr.equals(beginarr)) {
                if (nowLocal.compareTo(beginLocal) >= 0) {
                    monthCircle = nowLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                } else {
                    monthCircle = beginLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                }
            } else {
                if(nowLocal.compareTo(beginLocal) >= 0){
                    monthCircle = nowLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                }else {
                    monthCircle = beginLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                }
            }
        }
        map.put("beginDate",begin);
        map.put("endDate",end);

        String managerName = temp.get("managerName");
        String managerNumber = temp.get("managerNumber");
        map.put("manager",managerName+"-"+managerNumber);
        if(StringUtils.isNotEmpty(monthCircle)){
            map.put("monthCircle",monthCircle+"号");
        }else {
            map.put("monthCircle","");
        }
        return map;
    }

    public List<Map<String,String>> getProjectList(String number,String id, String month){
        try {
            List<Map<String, String>> listMap = mapper.getProjectListV2(number, id,OrgNumber);
            log.info(number+":"+JSONUtil.toJsonStr(listMap));
            List<Map<String, String>> resultList = new ArrayList<>();
            for (Map<String, String> temp : listMap) {
                Map<String, String> map = new HashMap<>();
                map.put("id", temp.get("orgId"));
                map.put("name", temp.get("orgName"));
                map.put("month", month);
                String begin = temp.get("beginDate");
                String end = temp.get("endDate");
                String beginarr = begin.substring(0, 7);
                String endarr = end.substring(0, 7);

                String monthCircle = "";
                String enter = temp.get("enterDate");
                String enterarr = enter.substring(0, 7);
                LocalDate nowLocal = LocalDate.now();
                LocalDate monthDate = LocalDate.parse(month+"-01",DateTimeFormatter.ISO_LOCAL_DATE);
                LocalDate enterDate = LocalDate.parse(enter,DateTimeFormatter.ISO_LOCAL_DATE);
                //相对日期
                if(enterarr.equals(month)){
                    nowLocal = enterDate;
                }else if(monthDate.compareTo(enterDate)>=0){
                    nowLocal = monthDate;
                }
                int num = nowLocal.getMonth().length(nowLocal.isLeapYear());

                LocalDate beginLocal = LocalDate.parse(begin, DateTimeFormatter.ISO_LOCAL_DATE);
                LocalDate endLocal = LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE);
                //判断结束时间是否等于现在时间
                // 不同年月，并且现在时间小于结束时间
                if ((!month.equals(endarr)) && nowLocal.compareTo(endLocal) < 0) {
                    if (month.equals(beginarr)) {
                        if (nowLocal.compareTo(beginLocal) >= 0) {
                            monthCircle = nowLocal.getDayOfMonth() + "-" + num;
                        } else {
                            monthCircle = beginLocal.getDayOfMonth() + "-" + num;
                        }
                    } else {
                        if (nowLocal.compareTo(beginLocal) >= 0) {
                            monthCircle = nowLocal.getDayOfMonth() + "-" + num;
                        } else {
                            num = beginLocal.getMonth().length(beginLocal.isLeapYear());
                            monthCircle = beginLocal.getDayOfMonth() + "-" + num;
                        }
                    }
                    //同年月，并且现在时间小于结束时间
                } else if (month.equals(endarr) && nowLocal.compareTo(endLocal) <= 0) {
                    if (month.equals(beginarr)) {
                        if (nowLocal.compareTo(beginLocal) >= 0) {
                            monthCircle = nowLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                        } else {
                            monthCircle = beginLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                        }
                    } else {
                        if (nowLocal.compareTo(beginLocal) >= 0) {
                            monthCircle = nowLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                        } else {
                            monthCircle = beginLocal.getDayOfMonth() + "-" + endLocal.getDayOfMonth();
                        }
                    }
                }
                map.put("beginDate", begin);
                map.put("endDate", end);

                String managerName = temp.get("managerName");
                String managerNumber = temp.get("managerNumber");
                map.put("manager", managerName + "-" + managerNumber);
                if (StringUtils.isNotEmpty(monthCircle)) {
                    map.put("monthCircle", monthCircle + "号");
                } else {
                    map.put("monthCircle", "");
                }
                log.info(JSONUtil.toJsonStr(map));
                resultList.add(map);
            }
            return resultList;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }
}
