package com.zcxk.evaluation.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.dict.DictUtil;
import com.zcxk.evaluation.dao.EvaluationItemMapper;
import com.zcxk.evaluation.entity.EvaluationItemEntity;
import com.zcxk.evaluation.entity.EvaluationItemValueEntity;
import com.zcxk.evaluation.service.EvaluationItemService;
import com.zcxk.evaluation.service.EvaluationItemValueService;
import com.zcxk.model.LoginUser;
import com.zcxk.org.OrgInfoUtil;
import com.zcxk.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 考评项Service接口
 *
 * @author: WYY
 * @date 2021-05-08 09:25
 */
@Service
public class EvaluationItemServiceImpl extends ServiceImpl<EvaluationItemMapper, EvaluationItemEntity> implements EvaluationItemService {

    @Resource
    private EvaluationItemMapper evaluationItemMapper;
    @Autowired
    private EvaluationItemValueService evaluationItemValueService;
    @Autowired
    private OrgInfoUtil orgInfoUtil;
    @Autowired
    private DictUtil dictUtil;
    /**
     * 自定义分页查询，含关联实体对像
     */
    @Override
    public IPage<EvaluationItemEntity> findPage(IPage<EvaluationItemEntity> page, EvaluationItemEntity evaluationItemEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            evaluationItemEntity.setTenantId(currentUser.getTenantId());
            evaluationItemEntity.setProgramItems(currentUser.getProgramItemList());
            evaluationItemEntity.setUserType(currentUser.getType());
            //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
            evaluationItemEntity.setPermissonType(currentUser.getPermissonType());
        }
        if(evaluationItemEntity.getDeptOrgId()!=null) {
            Integer tempCompanyId = orgInfoUtil.getOrgCompanyId(evaluationItemEntity.getDeptOrgId());
            if (tempCompanyId != null) {
                evaluationItemEntity.setCompanyOrgId(tempCompanyId);
            } else {
                evaluationItemEntity.setCompanyOrgId(evaluationItemEntity.getDeptOrgId());
                evaluationItemEntity.setDeptOrgId(null);
            }
        }
        Page<EvaluationItemEntity> iPage = evaluationItemMapper.findPage(page, evaluationItemEntity);
        for (EvaluationItemEntity item : iPage.getRecords()) {
            item.setCompanyOrgName(orgInfoUtil.getOrgName(item.getCompanyOrgId()));
            item.setDeptOrgName(orgInfoUtil.getOrgName(item.getDeptOrgId()));
            //考评周期
            String[] cycles = item.getCycle().split(",");
            for (String cycle : cycles) {
                item.setCycleName(item.getCycleName() == null ? dictUtil.getDictName("SC_EVALUATION_CYCLE ", Integer.valueOf(cycle)) : item.getCycleName() + "," + dictUtil.getDictName("SC_EVALUATION_CYCLE ", Integer.valueOf(cycle)));
            }
            //查询考评项分支
            EvaluationItemValueEntity entity = new EvaluationItemValueEntity();
            entity.setEvaluationItemId(item.getId());
            entity.setTenantId(currentUser.getTenantId());
            item.setItemValues(evaluationItemValueService.findList(entity));
        }
        return iPage;
    }
    /**
     * 查列表
     */
    @Override
    public List<EvaluationItemEntity> findList(EvaluationItemEntity evaluationItemEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if(currentUser!=null) {
            evaluationItemEntity.setTenantId(currentUser.getTenantId());
            evaluationItemEntity.setProgramItems(currentUser.getProgramItemList());
            evaluationItemEntity.setUserType(currentUser.getType());
            //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
            evaluationItemEntity.setPermissonType(currentUser.getPermissonType());
        }
        return evaluationItemMapper.findList(evaluationItemEntity);
    }

    /**
     * 批量删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delEvaluationItemByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 单个删除
     */
    @Override
    public boolean delEvaluationItemById(Long id) {
        boolean result = this.removeById(id);
        if (result) {
            evaluationItemValueService.updateByItemId(id);
        }
        return result;
    }

    /**
     * 保存
     */
    @Override
    public boolean addEvaluationItem(EvaluationItemEntity evaluationItem) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        for (Integer itemCompanyId : evaluationItem.getDeptOrgIds()) {
            Integer companyId = orgInfoUtil.getOrgCompanyId(itemCompanyId);
            if (companyId != null) {
                evaluationItem.setCompanyOrgId(companyId);
                evaluationItem.setDeptOrgId(itemCompanyId);
            } else {
                evaluationItem.setCompanyOrgId(itemCompanyId);
            }
            if (this.save(evaluationItem)) {
                Long id = evaluationItem.getId();
                for (EvaluationItemValueEntity item : evaluationItem.getItemValues()) {
                    if (currentUser != null) {
                        item.setTenantId(currentUser.getTenantId());
                        item.setCreateBy(currentUser.getUsername());
                        item.setUpdateBy(currentUser.getUsername());
                    }
                    item.setEvaluationItemId(id);
                    item.setStatus(1);
                    item.setDateUpdate(new Date());
                    item.setDateCreate(new Date());
                    evaluationItemValueService.addEvaluationItemValue(item);
                }
            }
        }
        return true;
    }

    /**
     * 修改根居ID
     */
    @Override
    public boolean updateEvaluationItemById(EvaluationItemEntity evaluationItem) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (this.updateById(evaluationItem)) {
            evaluationItemValueService.updateByItemId(evaluationItem.getId());
            for (EvaluationItemValueEntity item : evaluationItem.getItemValues()) {
                if (currentUser != null) {
                    item.setTenantId(currentUser.getTenantId());
                    item.setCreateBy(currentUser.getUsername());
                    item.setUpdateBy(currentUser.getUsername());
                }
                item.setEvaluationItemId(evaluationItem.getId());
                item.setStatus(1);
                item.setDateUpdate(new Date());
                item.setDateCreate(new Date());
                evaluationItemValueService.addEvaluationItemValue(item);
            }
            return true;
        }
        return false;
    }

    /**
     * 根居ID获取对象
     */
    @Override
    public EvaluationItemEntity findEvaluationItemById(Long id) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        EvaluationItemEntity entity = evaluationItemMapper.findEvaluationItemById(id);
        EvaluationItemValueEntity itemValueEntity = new EvaluationItemValueEntity();
        itemValueEntity.setEvaluationItemId(id);
        itemValueEntity.setTenantId(currentUser.getTenantId());
        entity.setItemValues(evaluationItemValueService.findList(itemValueEntity));
        return entity;
    }
}
