package com.yiqi.core.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.common.enums.UserStatus;
import com.yiqi.common.exception.GlobalException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.ClothWashModeRelation;
import com.yiqi.core.service.IClothWashModeRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothWashModeMapper;
import com.yiqi.core.domain.ClothWashMode;
import com.yiqi.core.service.IClothWashModeService;

/**
 * 洗衣方式Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-09
 */
@Service
public class ClothWashModeServiceImpl extends ServiceImpl<ClothWashModeMapper, ClothWashMode> implements IClothWashModeService {

    @Autowired
    IClothWashModeRelationService clothWashModeRelationService;

    /**
     * 查询洗衣方式
     *
     * @param id 洗衣方式主键
     * @return 洗衣方式
     */
    @Override
    public ClothWashMode selectClothWashModeById(Long id) {
        return baseMapper.selectClothWashModeById(id);
    }

    /**
     * 查询洗衣方式列表
     *
     * @param clothWashMode 洗衣方式
     * @return 洗衣方式
     */
    @Override
    public List<ClothWashMode> selectClothWashModeList(ClothWashMode clothWashMode) {
        return baseMapper.selectClothWashModeList(clothWashMode);
    }

    /**
     * 新增洗衣方式
     *
     * @param clothWashMode 洗衣方式
     * @return 结果
     */
    @Override
    public int insertClothWashMode(ClothWashMode clothWashMode) {
        clothWashMode.setCreateTime(DateUtils.getNowDate());
        clothWashMode.setCreateBy(SecurityUtils.getUsername());
        clothWashMode.setCreateById(SecurityUtils.getUserId());
        clothWashMode.setDelFlag(UserStatus.OK.getCode());
        clothWashMode.setStatus(UserStatus.OK.getCode());
        return baseMapper.insertClothWashMode(clothWashMode);
    }

    /**
     * 修改洗衣方式
     *
     * @param clothWashMode 洗衣方式
     * @return 结果
     */
    @Override
    public int updateClothWashMode(ClothWashMode clothWashMode) {
        clothWashMode.buildUpdateData();
        return baseMapper.updateClothWashMode(clothWashMode);
    }

    /**
     * 批量删除洗衣方式
     *
     * @param ids 需要删除的洗衣方式主键
     * @return 结果
     */
    @Override
    public int deleteClothWashModeByIds(Long[] ids) {
        long count = clothWashModeRelationService.count(new QueryWrapper<ClothWashModeRelation>().lambda()
                .in(ClothWashModeRelation::getWashModeId, (Object) ids));
        if (count>0){
            throw  new GlobalException("该洗衣方式存在关联的衣服类别,无法操作删除");
        }
        return baseMapper.deleteClothWashModeByIds(ids);
    }

    /**
     * 删除洗衣方式信息
     *
     * @param id 洗衣方式主键
     * @return 结果
     */
    @Override
    public int deleteClothWashModeById(Long id) {
        return baseMapper.deleteClothWashModeById(id);
    }

    @Override
    public int updateWashModeStatus(ClothWashMode clothWashMode) {
        clothWashMode.buildUpdateData();
        return baseMapper.updateClothWashMode(clothWashMode);
    }
}
