package com.ktg.iscs.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.core.text.Convert;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.bean.BeanUtils;
import com.ktg.iscs.domain.IsMaterials;
import com.ktg.iscs.domain.IsMaterialsRestitutionRules;
import com.ktg.iscs.domain.IsMaterialsType;
import com.ktg.iscs.domain.dto.materialsType.MaterialsTypePageDTO;
import com.ktg.iscs.domain.vo.materialsType.MaterialsTypePageVO;
import com.ktg.iscs.mapper.IsMaterialsTypeMapper;
import com.ktg.iscs.service.IIsMaterialsRestitutionRulesService;
import com.ktg.iscs.service.IIsMaterialsService;
import com.ktg.iscs.service.IIsMaterialsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 物资类型Service业务层处理
 *
 * @author cgj
 * @date 2024-11-08
 */
@Service
public class IsMaterialsTypeServiceImpl extends ServiceImpl<IsMaterialsTypeMapper, IsMaterialsType> implements IIsMaterialsTypeService
{
    @Autowired
    private IsMaterialsTypeMapper isMaterialsTypeMapper;
    @Autowired
    private IIsMaterialsService iIsMaterialsService;
    @Autowired
    private IIsMaterialsRestitutionRulesService iIsMaterialsRestitutionRulesService;

    /**
     * 查询物资类型
     *
     * @param materialsTypeId 物资类型主键
     * @return 物资类型
     */
    @Override
    public MaterialsTypePageVO selectIsMaterialsTypeByMaterialsTypeId(Long materialsTypeId)
    {
        return isMaterialsTypeMapper.selectIsMaterialsTypeByMaterialsTypeId(materialsTypeId);
    }

    /**
     * 查询物资类型列表
     *
     * @param isMaterialsType 物资类型
     * @return 物资类型
     */
    @Override
    public List<IsMaterialsType> selectIsMaterialsTypeList(IsMaterialsType isMaterialsType)
    {
        return isMaterialsTypeMapper.selectIsMaterialsTypeList(isMaterialsType);
    }

    /**
     * 新增物资类型
     *
     * @param dto 物资类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertIsMaterialsType(MaterialsTypePageDTO dto)
    {
        // 2.检查物资类型编码是否重复
        List<IsMaterialsType> list = list(Wrappers.<IsMaterialsType>lambdaQuery()
                .eq(IsMaterialsType::getMaterialsTypeCode, dto.getMaterialsTypeCode()));
        Assert.isFalse(!list.isEmpty(), "该物资类型编码已被使用!");
        Assert.isTrue(dto.getLoanDuration() >= dto.getReminderTime(), "借用时长需要大于等于提醒时长！");
        dto.setCreateTime(DateUtils.getNowDate());
        IsMaterialsType isMaterialsType = BeanUtils.toBean(dto, IsMaterialsType.class);
        save(isMaterialsType);
        // 开始新增规则
        IsMaterialsRestitutionRules isMaterialsRestitutionRules = new IsMaterialsRestitutionRules();
        isMaterialsRestitutionRules.setMaterialsTypeId(isMaterialsType.getMaterialsTypeId());
        isMaterialsRestitutionRules.setRestitutionRequired(dto.getRestitutionRequired());
        isMaterialsRestitutionRules.setRestorationRequired(dto.getRestorationRequired());
        isMaterialsRestitutionRules.setLoanDuration(dto.getLoanDuration());
        isMaterialsRestitutionRules.setReminderTime(dto.getReminderTime());
        isMaterialsRestitutionRules.setTimeoutAlarm(dto.getTimeoutAlarm());
        isMaterialsRestitutionRules.setDelFlag(dto.getDelFlag());
        iIsMaterialsRestitutionRulesService.insertIsMaterialsRestitutionRules(isMaterialsRestitutionRules);
        return 1;
    }

    /**
     * 修改物资类型
     *
     * @param dto 物资类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateIsMaterialsType(MaterialsTypePageDTO dto)
    {
        // 2.检查物资类型编码是否重复
        List<IsMaterialsType> list = list(Wrappers.<IsMaterialsType>lambdaQuery()
                .eq(IsMaterialsType::getMaterialsTypeCode, dto.getMaterialsTypeCode())
                .ne(IsMaterialsType::getMaterialsTypeId, dto.getMaterialsTypeId()));
        Assert.isFalse(!list.isEmpty(), "该物资类型编码已被使用!");
        Assert.isTrue(dto.getLoanDuration() >= dto.getReminderTime(), "借用时长需要大于等于提醒时长！");
        dto.setUpdateTime(DateUtils.getNowDate());
        IsMaterialsType isMaterialsType = BeanUtils.toBean(dto, IsMaterialsType.class);
        int i = isMaterialsTypeMapper.updateIsMaterialsType(isMaterialsType);
        // 开始修改规则
        IsMaterialsRestitutionRules one = iIsMaterialsRestitutionRulesService.getOne(Wrappers.<IsMaterialsRestitutionRules>lambdaQuery()
                .eq(IsMaterialsRestitutionRules::getMaterialsTypeId, dto.getMaterialsTypeId()));
        IsMaterialsRestitutionRules bean = BeanUtils.toBean(dto, IsMaterialsRestitutionRules.class);
        if (one != null) {
            bean.setRuleId(one.getRuleId());
            iIsMaterialsRestitutionRulesService.updateIsMaterialsRestitutionRules(bean);
        } else {
            iIsMaterialsRestitutionRulesService.insertIsMaterialsRestitutionRules(bean);
        }
        return i;
    }

    /**
     * 批量删除物资类型
     *
     * @param materialsTypeIds 需要删除的物资类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIsMaterialsTypeByMaterialsTypeIds(String materialsTypeIds)
    {
        Assert.notBlank(materialsTypeIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(materialsTypeIds);
        // 检查删除时下面有没有物资，有的话不可以删除
        List<IsMaterials> materialsList = iIsMaterialsService.list(Wrappers.<IsMaterials>lambdaQuery()
                .in(IsMaterials::getMaterialsTypeId, longIds));
        if (!materialsList.isEmpty()) {
            List<Long> materialsTypeIdList = materialsList.stream().map(IsMaterials::getMaterialsTypeId).collect(Collectors.toList());
            List<IsMaterialsType> isMaterialsTypes = listByIds(materialsTypeIdList);
            String collect = isMaterialsTypes.stream().map(IsMaterialsType::getMaterialsTypeName).collect(Collectors.joining(","));
            Assert.isTrue(false, collect + "分类下有物资，不可删除！");

        }
        // 查询有没有子类，有的话不能删除
        List<IsMaterialsType> materialsTypes = list(Wrappers.<IsMaterialsType>lambdaQuery()
                .in(IsMaterialsType::getParentId, materialsTypeIds));
        if (!materialsTypes.isEmpty()) {
            String collect = materialsTypes.stream().map(IsMaterialsType::getMaterialsTypeName).collect(Collectors.joining(","));
            Assert.isTrue(false, collect + "分类下有子类，不可删除！");

        }
        // 开始删除
        int i = isMaterialsTypeMapper.deleteIsMaterialsTypeByMaterialsTypeIds(longIds);
        // 开始删除rules
        List<IsMaterialsRestitutionRules> rulesList = iIsMaterialsRestitutionRulesService.list(Wrappers.<IsMaterialsRestitutionRules>lambdaQuery()
                .in(IsMaterialsRestitutionRules::getMaterialsTypeId, materialsTypeIds));
        if (!rulesList.isEmpty()) {
            String ids = rulesList.stream().map(IsMaterialsRestitutionRules::getRuleId).map(String::valueOf).collect(Collectors.joining(","));
            iIsMaterialsRestitutionRulesService.deleteIsMaterialsRestitutionRulesByRuleIds(ids);
        }
        return i;
    }

    /**
     * 删除物资类型信息
     *
     * @param materialsTypeId 物资类型主键
     * @return 结果
     */
    @Override
    public int deleteIsMaterialsTypeByMaterialsTypeId(Long materialsTypeId)
    {
        return isMaterialsTypeMapper.deleteIsMaterialsTypeByMaterialsTypeId(materialsTypeId);
    }

    @Override
    public Page<MaterialsTypePageVO> getIsMaterialsTypePage(Page<IsMaterialsType> page, IsMaterialsType isMaterialsType) {
        return isMaterialsTypeMapper.getIsMaterialsTypePage(page, isMaterialsType);
    }
}
