package com.bonus.material.scrap.service.impl;

import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.repair.domain.RepairPartDetails;
import com.bonus.material.scrap.domain.ScrapReason;
import com.bonus.material.scrap.mapper.ScrapReasonMapper;
import com.bonus.material.scrap.service.IScrapReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 报废原因Service业务层处理
 * 
 * @author hongchao
 * @date 2025-01-07
 */
@Service
public class ScrapReasonServiceImpl implements IScrapReasonService
{
    @Autowired
    private ScrapReasonMapper scrapReasonMapper;

    /**
     * 查询二级类型下拉框列表
     *
     * @param scrapReason 查询二级类型下拉框列表
     * @return 查询二级类型下拉框列表
     */
    @Override
    public List<ScrapReason> levelTwolist(ScrapReason scrapReason) {
        return scrapReasonMapper.levelTwolist(scrapReason);
    }

    /**
     * 查询三级类型下拉框列表
     *
     * @param scrapReason 查询三级类型下拉框列表
     * @return 查询三级类型下拉框列表
     */
    @Override
    public List<ScrapReason> levelThreelist(ScrapReason scrapReason) {
        return scrapReasonMapper.levelThreelist(scrapReason);
    }


    /**
     * 查询报废原因列表
     * 
     * @param scrapReason 报废原因管理
     * @return 报废原因管理
     */
    @Override
    public List<ScrapReason> selectScrapReasonList(ScrapReason scrapReason) {
        return scrapReasonMapper.selectScrapReasonList(scrapReason);
    }

    /**
     * 新增报废原因
     *
     * @param scrapReason 新增报废原因
     * @return 结果
     */
    @Override
    public int insertScrapReasonInfo(ScrapReason scrapReason)
    {
        scrapReason.setCreateTime(DateUtils.getNowDate());
        scrapReason.setUpdateTime(DateUtils.getNowDate());
        scrapReason.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserid()));
        scrapReason.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserid()));
        try {
            return scrapReasonMapper.insertScrapReasonInfo(scrapReason);
        } catch (Exception e) {
            throw new ServiceException("新增失败");
        }
    }

    /**
     * 获取报废原因管理详细信息
     *
     * @param id 获取报废原因管理详细信息
     * @return 获取报废原因管理详细信息
     */
    @Override
    public ScrapReason selectScrapReasonById(Long id) {
        try {
            return scrapReasonMapper.selectScrapReasonById(id);
        } catch (Exception e) {
            throw new ServiceException("查询失败");
        }
    }


    /**
     * 修改报废原因
     *
     * @param scrapReason 仓库管理
     * @return 结果
     */
    @Override
    public int updateScrapReasonInfo(ScrapReason scrapReason)
    {
        scrapReason.setUpdateTime(DateUtils.getNowDate());
        scrapReason.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserid()));
        try {
            // 先根据id查询报修记录，如果存在，修改报修记录的报废原因
            List<RepairPartDetails> list = scrapReasonMapper.getRepairRecord(scrapReason);
            if (!CollectionUtils.isEmpty(list)) {
                int res = scrapReasonMapper.updateRepairRecord(scrapReason);
                if (res == 0) {
                    throw new ServiceException("修改报废原因记录失败");
                }
            }
            return scrapReasonMapper.updateScrapReasonInfo(scrapReason);
        } catch (Exception e) {
            throw new ServiceException("修改失败");
        }
    }


    /**
     * 删除报废原因
     *
     * @param scrapReason 删除报废原因
     * @return 结果
     */
    @Override
    public int deleteScrapReasonInfo(ScrapReason scrapReason)
    {
        try {
            return scrapReasonMapper.deleteScrapReasonInfo(scrapReason);
        } catch (Exception e) {
            throw new ServiceException("删除失败");
        }
    }

    /**
     * 查询报废原因下拉列表 app用
     * @param scrapReason
     * @return
     */
    @Override
    public List<ScrapReason> getList(ScrapReason scrapReason) {
        return scrapReasonMapper.getList(scrapReason);
    }
}
