package com.qy.worksheetsystem.service.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qy.worksheetsystem.model.MessageResult;
import com.qy.worksheetsystem.model.mapper.PersonMapper;
import com.qy.worksheetsystem.model.mapper.PersonWorkMapper;

import com.qy.worksheetsystem.model.mapper.ProjectMapper;
import com.qy.worksheetsystem.service.PersonService;
import com.qy.worksheetsystem.service.PersonWorkService;
import com.qy.worksheetsystem.util.SnowflakeUtils;
import com.qy.worksheetsystem.vo.InsertJobWorkingVO;
import com.qy.worksheetsystem.vo.WorkHoursReport;
import com.qy.worksheetsystem.vo.WorkHoursReportEntry;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: workSheetSystem
 * @BelongsPackage: com.qy.worksheetsystem.service.impl
 * @Author: lhbj
 * @CreateTime: 2024-12-03  15:18
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class PersonWorkServiceImpl implements PersonWorkService {
    private static final BigDecimal h8 = new BigDecimal(8);

    @Resource
    private PersonWorkMapper personWorkMapper;
    @Resource
    private PersonService personService;

    @Resource
    private ProjectMapper projectMapper;

    @Override
    @Transactional
    public Map<String,Object> insertWork(Map<String,Object> param){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            int begin = (int) param.get("begin");
            int end = (int) param.get("end");
            String yearMonth = (String) param.get("yearMonth");
            WorkHoursReport jobWork = (WorkHoursReport) param.get("jobWork");
            Map<String, String> person = (Map<String, String>) param.get("person");
            Date beginDate = (Date) param.get("beginDate");
            Date endDate = (Date) param.get("endDate");
            Map<String, Map<String, Object>> availableMap = (Map<String, Map<String, Object>>) param.get("availableMap");

            List<Map<String, Object>> billList = Lists.newArrayList();
            List<Map<String, Object>> workList = Lists.newArrayList();
            Integer Hours = 0;
            for (WorkHoursReportEntry jobWorkEntry : jobWork.getEntries()) {
                String workingHours = jobWorkEntry.getWorkingHours();
                BigDecimal Hour = new BigDecimal(workingHours);
                Hours += Hour.intValue();
                String projectId = jobWorkEntry.getProjectId();

                String number = SnowflakeUtils.getInstance().getSnowflake().nextIdStr();

                Map<String, Object> billMap = new HashMap<>();

                String billID = personWorkMapper.selectBillKey();
                billMap.put("id", billID);
                billMap.put("affiliatedProject", projectId);
                billMap.put("fillingStartDate", beginDate);
                billMap.put("fillingEndDate", endDate);
                billMap.put("adminOrg", person.get("adminOrgID"));
                billMap.put("hrOrgUnit", person.get("hrOrgUnitID"));
                billMap.put("billState", jobWork.getState());
                billMap.put("number", number);
                billMap.put("CU", person.get("controlUnitID"));
                Date createTime = new Date();
                billMap.put("bizDate", createTime);
                billMap.put("creator", person.get("userID"));
                billMap.put("createTime", createTime);
                billMap.put("lastUpdateUser", person.get("userID"));
                billMap.put("lastUpdateTime", createTime);
                for (int i = begin; i <= end; i++) {
                    Map<String, Object> objectMap = Maps.newHashMap();
                    String iday = "01";
                    if (i < 10) {
                        iday = "0" + i;
                    } else {
                        iday = i + "";
                    }
                    String reportDate = yearMonth + iday;
                    Map<String, Object> avMap = availableMap.get(reportDate);
                    BigDecimal availableWorkHous = (BigDecimal) avMap.get("availableWorkHous");
                    BigDecimal workHours = (BigDecimal) avMap.get("workHours");
                    objectMap.put("parent", billID);
                    objectMap.put("employee", person.get("id"));
                    objectMap.put("clockTime", reportDate);
                    objectMap.put("clockLocation", "移动端");
                    objectMap.put("data", format.parse(reportDate));
                    objectMap.put("WorkingHours", availableWorkHous);
                    objectMap.put("notes", "");
                    objectMap.put("seq", i);
                    workList.add(objectMap);

                }
                billList.add(billMap);
            }

            int bInt = personWorkMapper.insertBill(billList);
            int wInt = personWorkMapper.insertWork(workList);
            Map<String, Object> reMap = Maps.newHashMap();
            reMap.put("billList", billList);
            reMap.put("workList", workList);
            return reMap;
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getAvailableDateInfo(String number, String startDate, String endDate) {
        Map<String,String> person = personService.getPersonByJobNo(number);
        List<Map<String,Object>> list = personWorkMapper.getAvailableDateInfo(person.get("id"),startDate,endDate);
        for(Map<String,Object> map : list){
            BigDecimal workHours = (BigDecimal) map.get("workHours");
            if(null!=workHours) {
                map.put("availableWorkHous", h8.subtract(workHours));
            }else{
                map.put("workHours", BigDecimal.ZERO);
                map.put("availableWorkHous", h8);
            }
        }
        return list;
    }

    public MessageResult getPersonWorkList(String number,String beginTime, String endTime, Boolean state) {
        return null;
    }


}
