package com.yiqi.core.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.common.enums.UserStatus;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.ClothFlaw;
import com.yiqi.core.mapper.ClothFlawMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothFlawTypeMapper;
import com.yiqi.core.domain.ClothFlawType;
import com.yiqi.core.service.IClothFlawTypeService;
import org.springframework.util.CollectionUtils;

/**
 * 衣服瑕疵类型Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@Service
public class ClothFlawTypeServiceImpl extends ServiceImpl<ClothFlawTypeMapper, ClothFlawType>  implements IClothFlawTypeService {

    @Autowired
    private ClothFlawMapper clothFlawMapper;

    /**
     * 查询衣服瑕疵类型
     *
     * @param id 衣服瑕疵类型主键
     * @return 衣服瑕疵类型
     */
    @Override
    public ClothFlawType selectClothFlawTypeById(Long id) {
        return baseMapper.selectClothFlawTypeById(id);
    }

    /**
     * 查询衣服瑕疵类型列表
     *
     * @param clothFlawType 衣服瑕疵类型
     * @return 衣服瑕疵类型
     */
    @Override
    public List<ClothFlawType> selectClothFlawTypeList(ClothFlawType clothFlawType) {
        return baseMapper.selectClothFlawTypeList(clothFlawType);
    }

    /**
     * 新增衣服瑕疵类型
     *
     * @param clothFlawType 衣服瑕疵类型
     * @return 结果
     */
    @Override
    public int insertClothFlawType(ClothFlawType clothFlawType) {
        clothFlawType.setCreateTime(DateUtils.getNowDate());
        clothFlawType.setCreateBy(SecurityUtils.getUsername());
        clothFlawType.setCreateById(SecurityUtils.getUserId());
        clothFlawType.setDelFlag(UserStatus.OK.getCode());
        clothFlawType.setStatus(UserStatus.OK.getCode());
        return baseMapper.insertClothFlawType(clothFlawType);
    }

    /**
     * 修改衣服瑕疵类型
     *
     * @param clothFlawType 衣服瑕疵类型
     * @return 结果
     */
    @Override
    public int updateClothFlawType(ClothFlawType clothFlawType) {
        clothFlawType.setUpdateTime(DateUtils.getNowDate());
        clothFlawType.setUpdateBy(SecurityUtils.getUsername());
        clothFlawType.setUpdateById(SecurityUtils.getUserId());
        return baseMapper.updateClothFlawType(clothFlawType);
    }

    /**
     * 批量删除衣服瑕疵类型
     *
     * @param ids 需要删除的衣服瑕疵类型主键
     * @return 结果
     */
    @Override
    public int deleteClothFlawTypeByIds(Long[] ids) {
        ClothFlaw flaw = new ClothFlaw();
        flaw.setFlawTypeId(ids[0]);
        if(!CollectionUtils.isEmpty(clothFlawMapper.selectClothFlawList(flaw))){
            throw new ServiceException("该瑕疵类型已绑定瑕疵,删除失败");
        }
        return baseMapper.deleteClothFlawTypeByIds(ids);
    }

    /**
     * 删除衣服瑕疵类型信息
     *
     * @param id 衣服瑕疵类型主键
     * @return 结果
     */
    @Override
    public int deleteClothFlawTypeById(Long id) {
        return baseMapper.deleteClothFlawTypeById(id);
    }

    @Override
    public int updateFlawTypeStatus(ClothFlawType clothFlawType) {
        clothFlawType.setUpdateTime(DateUtils.getNowDate());
        clothFlawType.setUpdateBy(SecurityUtils.getUsername());
        clothFlawType.setUpdateById(SecurityUtils.getUserId());
        return baseMapper.updateClothFlawType(clothFlawType);
    }
}
