package com.mortals.xhx.module.check.service.impl;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.common.code.CheckStatusEnum;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryVo;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryEntity;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryQuery;
import com.mortals.xhx.module.staff.service.StaffPerformSummaryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.check.dao.CheckReviewRecordDao;
import com.mortals.xhx.module.check.model.CheckReviewRecordEntity;
import com.mortals.xhx.module.check.service.CheckReviewRecordService;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* CheckReviewRecordService
* 评价差评绩效核查信息 service实现
*
* @author zxfei
* @date 2023-07-11
*/
@Service("checkReviewRecordService")
@Slf4j
public class CheckReviewRecordServiceImpl extends AbstractCRUDServiceImpl<CheckReviewRecordDao, CheckReviewRecordEntity, Long> implements CheckReviewRecordService {

    @Autowired
    private StaffPerformSummaryService staffPerformSummaryService;

    @Override
    protected void saveAfter(CheckReviewRecordEntity entity, Context context) throws AppException {
        try{
            StaffCheckSummaryQuery query = new StaffCheckSummaryQuery();
            query.setStaffId(entity.getStaffId());
            query.setCheckTimeStart(DateUtils.getStrDate(entity.getCheckTime()));
            query.setCheckTimeEnd(query.getCheckTimeStart());
            summaryCheck(query);
        }catch (Exception e){
            log.error("汇总已审核的核查记录出错",e);
        }
    }

    @Override
    public void examine(CheckReviewRecordEntity entity, Context context) throws AppException {
        if(entity.getId()==null){
            throw new AppException("核查记录ID不能为空");
        }
        if (context != null && context.getUser()!=null) {
            IUser user = context.getUser();
            entity.setUpdateUserId(user.getId());
            entity.setCheckPerson(user.getRealName());
        }
        entity.setCheckTime(new Date());
        entity.setUpdateTime(entity.getCheckTime());
        entity.setCheckStatus(CheckStatusEnum.已处理.getValue()); //处理状态(1.未处理,2.已处理)
        dao.update(entity);
        try{
            CheckReviewRecordEntity temp = this.get(entity.getId());
            if(temp!=null) {
                StaffCheckSummaryQuery query = new StaffCheckSummaryQuery();
                query.setStaffId(temp.getStaffId());
                summaryCheck(query);
            }
        }catch (Exception e){
            log.error("汇总已审核的核查记录出错",e);
        }
    }

    @Override
    public List<StaffCheckSummaryVo> summaryCheck(StaffCheckSummaryQuery query) throws AppException {
        if(StringUtils.isEmpty(query.getCheckTimeStart())||StringUtils.isEmpty(query.getCheckTimeStart())){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar=Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String startTime = format.format(calendar.getTime());
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            String endTime = format.format(calendar.getTime());
            query.setCheckTimeStart(startTime);
            query.setCheckTimeEnd(endTime);
        }
        List<StaffCheckSummaryVo> summaryVoList = dao.summaryCheck(query);
        if(CollectionUtils.isNotEmpty(summaryVoList)){
            for(StaffCheckSummaryVo vo:summaryVoList){
                StaffPerformSummaryEntity staffPerformSummaryEntity = new StaffPerformSummaryEntity();
                BeanUtils.copyProperties(vo,staffPerformSummaryEntity, BeanUtil.getNullPropertyNames(vo));
                staffPerformSummaryEntity.setAssessScore(vo.getSumScore());
                StaffPerformSummaryQuery summaryQuery = new StaffPerformSummaryQuery();
                summaryQuery.setStaffId(vo.getStaffId());
                summaryQuery.setYear(vo.getYear());
                summaryQuery.setMonth(vo.getMonth());
                StaffPerformSummaryEntity temp = staffPerformSummaryService.selectOne(summaryQuery);
                if(temp!=null){
                    if(temp.getAttendScore()==null){
                        staffPerformSummaryEntity.setAttendScore(new BigDecimal(0));
                    }
                    if(temp.getOtherScore()==null){
                        staffPerformSummaryEntity.setOtherScore(new BigDecimal(0));
                    }
                    if(temp.getWorkScore()==null){
                        staffPerformSummaryEntity.setWorkScore(new BigDecimal(0));
                    }
                    if(temp.getEffectScore()==null){
                        staffPerformSummaryEntity.setEffectScore(new BigDecimal(0));
                    }
                    BigDecimal erro = new BigDecimal(0);
                    erro.add(staffPerformSummaryEntity.getAssessScore());
                    erro.add(staffPerformSummaryEntity.getAttendScore());
                    erro.add(staffPerformSummaryEntity.getOtherScore());
                    erro.add(staffPerformSummaryEntity.getWorkScore());
                    erro.add(staffPerformSummaryEntity.getEffectScore());
                    staffPerformSummaryEntity.setErrorScore(erro);
                    BigDecimal total = new BigDecimal(100);
                    staffPerformSummaryEntity.setTotalScore(total.subtract(erro));
                    staffPerformSummaryEntity.setId(temp.getId());
                    staffPerformSummaryEntity.setUpdateTime(new Date());
                    staffPerformSummaryService.update(staffPerformSummaryEntity);
                    staffPerformSummaryEntity.setId(temp.getId());
                    staffPerformSummaryEntity.setUpdateTime(new Date());
                    staffPerformSummaryService.update(staffPerformSummaryEntity);
                }else {
                    staffPerformSummaryEntity.setAttendScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setOtherScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setWorkScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setEffectScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setErrorScore(vo.getSumScore());
                    BigDecimal total = new BigDecimal(100);
                    staffPerformSummaryEntity.setTotalScore(total.subtract(vo.getSumScore()));
                    staffPerformSummaryEntity.setCreateUserId(1l);
                    staffPerformSummaryEntity.setCreateTime(new Date());
                    staffPerformSummaryService.save(staffPerformSummaryEntity);
                }

            }
        }
        return summaryVoList;
    }
}