package com.yiqi.core.service.impl;

import java.util.List;

import com.yiqi.common.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.common.core.domain.entity.SysUser;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.dto.ClothFlawTopDTO;
import com.yiqi.core.domain.vo.ClothFlawTopVo;
import com.yiqi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothFlawTopMapper;
import com.yiqi.core.domain.ClothFlawTop;
import com.yiqi.core.service.IClothFlawTopService;

/**
 * 衣服瑕疵置顶Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-08
 */
@Service
public class ClothFlawTopServiceImpl extends ServiceImpl<ClothFlawTopMapper, ClothFlawTop> implements IClothFlawTopService {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询衣服瑕疵置顶
     *
     * @param id 衣服瑕疵置顶主键
     * @return 衣服瑕疵置顶
     */
    @Override
    public ClothFlawTop selectClothFlawTopById(Long id) {
        return baseMapper.selectClothFlawTopById(id);
    }

    /**
     * 查询衣服瑕疵置顶列表
     *
     * @param clothFlawTop 衣服瑕疵置顶
     * @return 衣服瑕疵置顶
     */
    @Override
    public List<ClothFlawTopVo> selectClothFlawTopList(ClothFlawTop clothFlawTop) {
        //获取登录人信息
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        clothFlawTop.setSourceType(sysUser.getUserType());
        if(sysUser.getUserType().equals(SourceType.FACTORY.getCode())){
            clothFlawTop.setTargetId(sysUser.getFactoryId() == null ? sysUser.getStoreId() : sysUser.getFactoryId());
        }else if(sysUser.getUserType().equals(SourceType.STORE.getCode())){
            clothFlawTop.setTargetId(sysUser.getFactoryId() == null ? sysUser.getStoreId() : sysUser.getFactoryId());
        }else{
            throw new ServiceException("总部身份无法配置瑕疵置顶");
        }

        return baseMapper.selectClothFlawTopList(clothFlawTop);
    }

    /**
     * 修改衣服瑕疵置顶
     *
     * @return 结果
     */
    @Override
    public int updateClothFlawTop(ClothFlawTopDTO clothFlawTopDTO) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        clothFlawTopDTO.setSourceType(sysUser.getUserType());
        clothFlawTopDTO.setTargetId(sysUser.getFactoryId() == null ? sysUser.getStoreId() : sysUser.getFactoryId());
        //获取登录人信息
        if(clothFlawTopDTO.getIsCheck() == 0){
            return baseMapper.deleteClothFlawTop(clothFlawTopDTO);
        }else {
            ClothFlawTop clothFlawTop = new ClothFlawTop();
            BeanUtils.copyProperties(clothFlawTopDTO,clothFlawTop);
            clothFlawTop.setCreateTime(DateUtils.getNowDate());
            clothFlawTop.setCreateBy(SecurityUtils.getUsername());
            clothFlawTop.setCreateById(SecurityUtils.getUserId());
            return baseMapper.insertClothFlawTop(clothFlawTop);
        }
    }

}
