package com.tofly.xrtygis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.xrtygis.entity.TfKpiManage;
import com.tofly.xrtygis.entity.TfKpiPersonManage;
import com.tofly.xrtygis.entity.TfKpiStandardManage;
import com.tofly.xrtygis.entity.dto.TfKpiPersonManageDto;
import com.tofly.xrtygis.mapper.TfKpiManageMapper;
import com.tofly.xrtygis.mapper.TfKpiPersonManageMapper;
import com.tofly.xrtygis.service.TfKpiManageService;
import com.tofly.xrtygis.service.TfKpiPersonManageService;
import com.tofly.xrtygis.service.TfKpiStandardManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 绩效考核人员管理
 *
 * @author JF
 * @date Fri Mar 25 00:00:00 CST 2022
 */
@Service("tfKpiPersonManageService")
public class TfKpiPersonManageServiceImpl extends ServiceImpl<TfKpiPersonManageMapper, TfKpiPersonManage> implements TfKpiPersonManageService {

    @Autowired
    private TfKpiStandardManageService tfKpiStandardManageService;

    @Override
    public boolean saveTfKpiPersonManage(TfKpiPersonManageDto tfKpiPersonManageDto) {

        TfKpiPersonManage tfKpiPersonManage = tfKpiPersonManageDto.getTfKpiPersonManage();
        if(tfKpiPersonManage!=null){

            boolean hasAssessDuty = tfKpiPersonManage.getAssessDuty() != null;
            boolean hasAreaType = tfKpiPersonManage.getAssessType() != null;
            boolean hasAssessPerson = tfKpiPersonManage.getAssessPerson() != null;
            String[] assessDMAs = tfKpiPersonManageDto.getAssessDMA().split(",");
            String[] assessContents = tfKpiPersonManageDto.getAssessContentMore().split(",");

            for(String assessDMA:assessDMAs){
                for (String assessContent:assessContents){
                    boolean hasAssessContent = assessContent != null;
                    boolean hasAssessDMA = assessDMA != null;
                    TfKpiPersonManage tfKpiPerson = this.getOne(new QueryWrapper<TfKpiPersonManage>()
                            .eq(hasAssessDMA,"ASSESS_AREA",assessDMA)
                            .eq(hasAssessDuty,"ASSESS_DUTY",tfKpiPersonManage.getAssessDuty())
                            .eq(hasAssessContent,"ASSESS_CONTENT",assessContent)
                            .eq(hasAreaType,"ASSESS_TYPE",tfKpiPersonManage.getAssessType())
                            .eq(hasAssessPerson,"ASSESS_PERSON",tfKpiPersonManage.getAssessPerson()));
                    if(tfKpiPerson!=null){
                        continue;
                    }

                    //查询出考核标准
                    TfKpiStandardManage tfKpiStandardManage = tfKpiStandardManageService.getOne(new QueryWrapper<TfKpiStandardManage>()
                            .eq(hasAssessContent,"ASSESS_CONTENT",assessContent)
                            .eq(hasAreaType,"ASSESS_TYPE",tfKpiPersonManage.getAssessType()));
                    if(tfKpiStandardManage.getAssessStandard()!=null){
                        tfKpiPersonManage.setAssessStandard(tfKpiStandardManage.getAssessStandard());
                    }else {
                        continue;
                    }
                    tfKpiPersonManage.setAssessArea(assessDMA);
                    tfKpiPersonManage.setAssessContent(assessContent);
                    this.save(tfKpiPersonManage);
                }
            }
        }else {
            return false;
        }
        return true;
    }
}
