package com.engine.salary.biz;

import com.engine.salary.entity.sicategory.bo.ICategoryBO;
import com.engine.salary.entity.sicategory.dto.ICategoryFormDTO;
import com.engine.salary.entity.sicategory.po.ICategoryPO;
import com.engine.salary.entity.sischeme.po.InsuranceSchemeDetailPO;
import com.engine.salary.enums.sicategory.IsPaymentEnum;
import com.engine.salary.enums.sicategory.WelfareTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.sicategory.ICategoryMapper;
import com.engine.salary.util.SalaryEnumUtil;
import com.mzlion.core.utils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import weaver.conn.mybatis.MyBatisFactory;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author weaver_cl
 * @Description:
 * @Date 2022/3/9
 * @Version V1.0
 **/
public class SICategoryBiz {


    /**
     * 自定义福利表单
     * id == null ? 新建表单 ： 查看已有数据内容表单
     * @param id         自定义福利主键
     * @return 表单
     */
    public ICategoryFormDTO getForm(Long id) {
        if (id != null) {
            ICategoryPO iCategoryPO = getByID(id);
            ICategoryFormDTO iCategoryFormDTO = new ICategoryFormDTO();
            if (Objects.isNull(iCategoryPO)) {
                throw new SalaryRunTimeException("数据不存在");
            }
            BeanUtils.copyProperties(iCategoryPO,iCategoryFormDTO);
            iCategoryFormDTO.setWelfareType(SalaryEnumUtil.enumMatchByValue(iCategoryPO.getWelfareType(), WelfareTypeEnum.values(), WelfareTypeEnum.class));
            iCategoryFormDTO.setPaymentScope(SalaryEnumUtil.stringToEnums(iCategoryPO.getPaymentScope(), ","));
            return iCategoryFormDTO;
        }

        return ICategoryFormDTO.builder().welfareType(WelfareTypeEnum.SOCIAL_SECURITY).build();
    }


    /**
     * 根据id获取
     * @param id
     * @return
     */
    public ICategoryPO getByID(Long id) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            ICategoryMapper iCategoryMapper = sqlSession.getMapper(ICategoryMapper.class);
            ICategoryPO iCategoryPO = iCategoryMapper.getById(id);
            return  iCategoryPO;

        } finally {
            sqlSession.close();
        }
    }

    /**
     * 保存
     * @param iCategoryFormDTO
     * @param employeeId DataTypeEnum.SYSTEM.getValue()
     */
    public void save(ICategoryFormDTO iCategoryFormDTO, long employeeId) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            ICategoryMapper iCategoryMapper = sqlSession.getMapper(ICategoryMapper.class);
            iCategoryFormDTO.setInsuranceName(StringUtils.trim(iCategoryFormDTO.getInsuranceName()));
            List<ICategoryPO> iCategoryPOS = listByName(iCategoryFormDTO.getInsuranceName());
            if (CollectionUtils.isNotEmpty(iCategoryPOS)) {
                throw new SalaryRunTimeException("福利名称不允许重复");
            }
            ICategoryPO iCategoryPO = ICategoryBO.convertToInsuranceCategoryPO(iCategoryFormDTO, employeeId);
            iCategoryMapper.insert(iCategoryPO);

            sqlSession.commit();

        } finally {
            sqlSession.close();
        }


    }

    /**
     * 根据名称获取
      * @param insuranceName
     * @return
     */
    public List<ICategoryPO> listByName(String insuranceName) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();

        try{
            ICategoryMapper iCategoryMapper = sqlSession.getMapper(ICategoryMapper.class);
            List<ICategoryPO> iCategoryPOS = iCategoryMapper.listByName(insuranceName);
            return iCategoryPOS;

        } finally {
            sqlSession.close();
        }
    }

    /**
     * 更新
     * @param iCategoryFormDTO
     * @param employeeId
     */
    public void update(ICategoryFormDTO iCategoryFormDTO, long employeeId) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try{
            if (iCategoryFormDTO.getId() == null) {
                throw new SalaryRunTimeException("id is required");
            }

            ICategoryPO iCategoryPO = getByID(iCategoryFormDTO.getId());
            if (Objects.isNull(iCategoryPO)) {
                throw new SalaryRunTimeException("数据不存在");
            }

            List<ICategoryPO> iCategoryPOS = listByName(iCategoryFormDTO.getInsuranceName());
            if (CollectionUtils.isNotEmpty(iCategoryPOS)) {
                throw new SalaryRunTimeException("福利名称不允许重复");
            }

            iCategoryPO.setInsuranceName(iCategoryFormDTO.getInsuranceName());
//            iCategoryPO.setWelfareType(iCategoryFormDTO.getWelfareType().getValue());
//            iCategoryPO.setPaymentScope(SalaryEnumUtil.enumArrToString(iCategoryFormDTO.getPaymentScope()));
            iCategoryPO.setUpdateTime(new Date());
            ICategoryMapper iCategoryMapper = sqlSession.getMapper(ICategoryMapper.class);
            iCategoryMapper.update(iCategoryPO);

            sqlSession.commit();
        } finally {
            sqlSession.close();
        }

    }

    /**
     * 更新状态
     * @param id
     * @param isUse
     * @param employeeId
     */
    public void updateStatusById(Long id, Integer isUse, long employeeId) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            if(id == null) {
                throw new SalaryRunTimeException("id is required");
            }
            if (isUse == null) {
                throw new SalaryRunTimeException("isUse is required");
            }
            List<InsuranceSchemeDetailPO> insuranceSchemeDetailPOS = new SISchemeBiz().queryListByInsuranceIdIsPayment(id, IsPaymentEnum.YES.getValue());
            if(CollectionUtils.isNotEmpty(insuranceSchemeDetailPOS) && isUse == 0) {
                throw new SalaryRunTimeException("该福利开启缴费，不可删除(或停用)");
            }
            ICategoryPO iCategoryPO = getByID(id);
            if (Objects.isNull(iCategoryPO)) {
                throw new SalaryRunTimeException("数据记录不存在");
            }
            iCategoryPO.setIsUse(isUse);
            ICategoryMapper iCategoryMapper = sqlSession.getMapper(ICategoryMapper.class);
            iCategoryMapper.updateById(iCategoryPO);

            sqlSession.commit();
        }finally {
            sqlSession.close();
        }

    }
}
