package com.weaver.seconddev.chapanda.beisen.service.impl;

import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.teams.domain.user.SimpleEmployee;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weaver.seconddev.chapanda.beisen.dao.ConversionResignDao;
import com.weaver.seconddev.chapanda.beisen.service.ConversionResignService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;


/***
 * author:shil
 */
@Service
public class ConversionResignServiceImpl implements ConversionResignService {

    private final static Logger log = LoggerFactory.getLogger(ConversionResignServiceImpl.class);

    @Autowired
    private ConversionResignDao conversionResignDao;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    /**
     * 
     * @param paraMap
     * @return
     */
    @Override
    public Map<String, Object> resignConversion(Map<String, Object> paraMap) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String conversionDay = "";


        Map<String, Object> dataMap = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long employeeId = Long.valueOf(String.valueOf(paraMap.get("employeeId")));
        String lzrq = (String) paraMap.get("lzrq");
        log.error("employeeId:"+employeeId);
        log.error("lzrq:{}",lzrq);

        String hiredate = "";
        if(employeeId > 0){
            SimpleEmployee simpleEmployee = hrmCommonUtil.getSimpleEmployee(employeeId);
            hiredate = sdf.format(simpleEmployee.getHiredate());
        }
        log.error("hiredate:{}",hiredate);


        dataMap.put("employeeid",employeeId);
        dataMap.put("lzrq",lzrq);
        dataMap.put("rzrq",hiredate);

        if(StringUtils.isNotBlank(lzrq) && StringUtils.isNotBlank(hiredate)){
            String rznf = hiredate.substring(0,4);
            String lznf = lzrq.substring(0,4);

            log.error("rznf:"+rznf);
            log.error("lznf:"+lznf);

            if(StringUtils.isNotBlank(rznf) && StringUtils.isNotBlank(lznf) ){
                double annualBase = conversionResignDao.queryEmployeeAnnualLeave(String.valueOf(employeeId),lznf);

                log.error("annualBase:"+annualBase);

                dataMap.put("annualBase",annualBase+"");
                if(rznf.equals(lznf)){
                    LocalDate rzDate = LocalDate.parse(hiredate, formatter);
                    LocalDate lzDate = LocalDate.parse(lzrq, formatter);
                    long workDays = ChronoUnit.DAYS.between(lzDate, rzDate);
                    dataMap.put("workdays",workDays+"");
                    if(annualBase !=0){
                        conversionDay = new BigDecimal(Double.toString((workDays/365.0)*annualBase)).setScale(2, RoundingMode.HALF_UP).toString();
                    }
                }else{
                    LocalDate rzDate = LocalDate.parse(lznf+"-01-01", formatter);
                    LocalDate lzDate = LocalDate.parse(lzrq, formatter);
                    long workDays = ChronoUnit.DAYS.between(lzDate, rzDate);
                    dataMap.put("workdays",workDays+"");
                    if(annualBase !=0){
                        conversionDay = new BigDecimal(Double.toString((workDays/365.0)*annualBase)).setScale(2, RoundingMode.HALF_UP).toString();
                    }
                }
            }
        }
        dataMap.put("days",conversionDay);
        return dataMap;
    }

    /***
     *
     * @param paraMap
     * @return
     */
    @Override
    public Map<String, Object> resignConversion2(Map<String, Object> paraMap) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String conversionDay = "";


        Map<String, Object> dataMap = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long employeeId = Long.valueOf(String.valueOf(paraMap.get("employeeId")));
        String lzrq = (String) paraMap.get("lzrq");
        log.error("employeeId:"+employeeId);
        log.error("lzrq:"+lzrq);

        String hiredate = "";
        if(employeeId > 0){
            hiredate = conversionResignDao.queryEmployeeById(String.valueOf(employeeId));
        }
        if(StringUtils.isNotBlank(hiredate)){
            hiredate = hiredate.substring(0,10);
        }
        log.error("hiredate:"+hiredate);

        dataMap.put("employeeid",employeeId);
        dataMap.put("lzrq",lzrq);
        dataMap.put("rzrq",hiredate);

        if(StringUtils.isNotBlank(lzrq) && StringUtils.isNotBlank(hiredate)){

            String rznf = hiredate.substring(0,4);
            String lznf = lzrq.substring(0,4);

            log.error("rznf:"+rznf);
            log.error("lznf:"+lznf);

            if(StringUtils.isNotBlank(rznf) && StringUtils.isNotBlank(lznf) ){
                double annualBase = conversionResignDao.queryEmployeeAnnualLeave(String.valueOf(employeeId),lznf);

                log.error("annualBase:"+annualBase);

                dataMap.put("annualBase",annualBase+"");
                if(rznf.equals(lznf)){
                    LocalDate rzDate = LocalDate.parse(hiredate, formatter);
                    LocalDate lzDate = LocalDate.parse(lzrq, formatter);
                    long workDays = ChronoUnit.DAYS.between(rzDate,lzDate);
                    dataMap.put("workdays",workDays+"");
                    if(annualBase !=0){
                        conversionDay = new BigDecimal(Double.toString((workDays/365.0)*annualBase)).setScale(2, RoundingMode.HALF_UP).toString();
                    }
                }else{
                    LocalDate rzDate = LocalDate.parse(lznf+"-01-01", formatter);
                    LocalDate lzDate = LocalDate.parse(lzrq, formatter);
                    long workDays = ChronoUnit.DAYS.between(rzDate,lzDate);
                    dataMap.put("workdays",workDays+"");
                    if(annualBase !=0){
                        conversionDay = new BigDecimal(Double.toString((workDays/365.0)*annualBase)).setScale(2, RoundingMode.HALF_UP).toString();
                    }
                }
            }
        }
        dataMap.put("days",conversionDay);
        return dataMap;

    }
}
