package com.yiqi.core.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.GlobalException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.ClothRewashReason;
import com.yiqi.core.service.IClothRewashReasonService;
import com.yiqi.system.domain.SysStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothRewashReasonTypeMapper;
import com.yiqi.core.domain.ClothRewashReasonType;
import com.yiqi.core.service.IClothRewashReasonTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 衣服重洗原因类别Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-23
 */
@Service
public class ClothRewashReasonTypeServiceImpl extends ServiceImpl<ClothRewashReasonTypeMapper,ClothRewashReasonType> implements IClothRewashReasonTypeService {

    @Autowired
    IClothRewashReasonService clothRewashReasonService;
    /**
     * 查询衣服重洗原因类别
     *
     * @param id 衣服重洗原因类别主键
     * @return 衣服重洗原因类别
     */
    @Override
    public ClothRewashReasonType selectClothRewashReasonTypeById(Long id) {
        return baseMapper.selectClothRewashReasonTypeById(id);
    }

    /**
     * 查询衣服重洗原因类别列表
     *
     * @param clothRewashReasonType 衣服重洗原因类别
     * @return 衣服重洗原因类别
     */
    @Override
    public List<ClothRewashReasonType> selectClothRewashReasonTypeList(ClothRewashReasonType clothRewashReasonType) {
        return baseMapper.selectClothRewashReasonTypeList(clothRewashReasonType);
    }

    /**
     * 新增衣服重洗原因类别
     *
     * @param clothRewashReasonType 衣服重洗原因类别
     * @return 结果
     */
    @Override
    public int insertClothRewashReasonType(ClothRewashReasonType clothRewashReasonType) {
        ClothRewashReasonType type = baseMapper.selectOne(new QueryWrapper<ClothRewashReasonType>().lambda()
                .eq(ClothRewashReasonType::getName, clothRewashReasonType.getName()));
        if (type != null){
            throw  new GlobalException("存在该类别,无法创建");
        }
        clothRewashReasonType.setCreateTime(DateUtils.getNowDate());
        clothRewashReasonType.setDelFlag("0");
        return baseMapper.insertClothRewashReasonType(clothRewashReasonType);
    }

    /**
     * 修改衣服重洗原因类别
     *
     * @param clothRewashReasonType 衣服重洗原因类别
     * @return 结果
     */
    @Override
    public int updateClothRewashReasonType(ClothRewashReasonType clothRewashReasonType) {
        List<ClothRewashReasonType> types = baseMapper.selectList(new QueryWrapper<ClothRewashReasonType>().lambda()
                .eq(ClothRewashReasonType::getName, clothRewashReasonType.getName())
                .ne(ClothRewashReasonType::getId, clothRewashReasonType.getId()));
        if (CollUtil.isNotEmpty(types)){
            throw  new GlobalException("存在该类别,无法修改");
        }
        clothRewashReasonType.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateClothRewashReasonType(clothRewashReasonType);
    }

    /**
     * 批量删除衣服重洗原因类别
     *
     * @param ids 需要删除的衣服重洗原因类别主键
     * @return 结果
     */
    @Override
    public int deleteClothRewashReasonTypeByIds(Long[] ids) {
        for (Long id : ids) {
            ClothRewashReason one = clothRewashReasonService.getOne(new QueryWrapper<ClothRewashReason>().lambda()
                    .eq(ClothRewashReason::getDelFlag, "0")
                    .eq(ClothRewashReason::getRewashTypeId, id)
                    .last("limit 1")
            );
            if (one != null){
                throw  new GlobalException("存在该类别的重洗原因,无法删除, 类别编号为 " + one.getRewashTypeId().toString())  ;
            }
        }
        return baseMapper.deleteClothRewashReasonTypeByIds(ids);
    }

    /**
     * 删除衣服重洗原因类别信息
     *
     * @param id 衣服重洗原因类别主键
     * @return 结果
     */
    @Override
    public int deleteClothRewashReasonTypeById(Long id) {
        ClothRewashReason one = clothRewashReasonService.getOne(new QueryWrapper<ClothRewashReason>().lambda()
                        .eq(ClothRewashReason::getDelFlag, "0")
                        .eq(ClothRewashReason::getRewashTypeId, id)
                );
        if (one != null){
           throw  new GlobalException("存在该类别的重洗原因,无法删除")  ;
        }
        return baseMapper.deleteClothRewashReasonTypeById(id);
    }



}
