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

import java.util.List;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.purchase.domain.dto.PurchaseCheckFileDto;
import com.bonus.material.purchase.mapper.PurchaseCheckFileMapper;
import org.springframework.stereotype.Service;
import com.bonus.material.purchase.mapper.PurchaseCheckDetailsMapper;
import com.bonus.material.purchase.domain.PurchaseCheckDetails;
import com.bonus.material.purchase.service.IPurchaseCheckDetailsService;

import javax.annotation.Resource;

/**
 * 新购验收任务详细Service业务层处理
 * 
 * @author xsheng
 * @date 2024-10-16
 */
@Service
public class PurchaseCheckDetailsServiceImpl implements IPurchaseCheckDetailsService {

    @Resource
    private PurchaseCheckDetailsMapper purchaseCheckDetailsMapper;

    @Resource
    private PurchaseCheckFileMapper purchaseCheckFileMapper;

    /**
     * 查询新购验收任务详细
     * 
     * @param id 新购验收任务详细主键
     * @return 新购验收任务详细
     */
    @Override
    public PurchaseCheckDetails selectPurchaseCheckDetailsById(Long id) {
        return purchaseCheckDetailsMapper.selectPurchaseCheckDetailsById(id);
    }

    /**
     * 查询新购验收任务详细列表
     * 
     * @param purchaseCheckDetails 新购验收任务详细
     * @return 新购验收任务详细
     */
    @Override
    public List<PurchaseCheckDetails> selectPurchaseCheckDetailsList(PurchaseCheckDetails purchaseCheckDetails) {
        return purchaseCheckDetailsMapper.selectPurchaseCheckDetailsList(purchaseCheckDetails);
    }

    /**
     * 根据任务ID查询报告附件列表
     *
     * @param taskType 任务类型
     */
    @Override
    public List<PurchaseCheckFileDto> selectPurchaseCheckFileListByTaskType(Long taskType, Long typeId) {
        try {
            return purchaseCheckFileMapper.selectPurchaseCheckFileListByTaskType(taskType, typeId);
        } catch (Exception e) {
            throw new RuntimeException("SQL执行错误:" + e.getMessage());
        }
    }

    /**
     * 新增报告附件
     *
     * @param purchaseCheckFileDto 附件信息
     */
    @Override
    public AjaxResult insertPurchaseCheckFile(PurchaseCheckFileDto purchaseCheckFileDto) {
        try {
            if (purchaseCheckFileDto != null) {
                purchaseCheckFileDto.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
            }
            return purchaseCheckFileMapper.insertPurchaseCheckFiles(purchaseCheckFileDto) > 0 ? AjaxResult.success() : AjaxResult.error("SQL插入0条");
        } catch (Exception e) {
            return AjaxResult.error("SQL执行错误:" + e.getMessage());
        }
    }

    /**
     * 新增新购验收任务详细
     * 
     * @param purchaseCheckDetails 新购验收任务详细
     * @return 结果
     */
    @Override
    public int insertPurchaseCheckDetails(PurchaseCheckDetails purchaseCheckDetails) {
        purchaseCheckDetails.setCreateTime(DateUtils.getNowDate());
        try {
            return purchaseCheckDetailsMapper.insertPurchaseCheckDetails(purchaseCheckDetails);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 修改新购验收任务详细
     * 
     * @param purchaseCheckDetails 新购验收任务详细
     * @return 结果
     */
    @Override
    public int updatePurchaseCheckDetails(PurchaseCheckDetails purchaseCheckDetails) {
        purchaseCheckDetails.setUpdateTime(DateUtils.getNowDate());
        try {
            return purchaseCheckDetailsMapper.updatePurchaseCheckDetails(purchaseCheckDetails);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 批量修改新购验收任务详细状态 -- 批量、限制状态
     *
     * @param taskId        任务id
     * @param newTaskStatus 新状态
     */
    @Override
    public int batchUpdateDetailsTaskStatus(Long taskId, Integer newTaskStatus, String checkResult) {
        return purchaseCheckDetailsMapper.batchUpdateDetailsTaskStatus(taskId, newTaskStatus, checkResult);
    }

    /**
     * 批量删除新购验收任务详细
     * 
     * @param ids 需要删除的新购验收任务详细主键
     * @return 结果
     */
    @Override
    public int deletePurchaseCheckDetailsByIds(Long[] ids) {
        return purchaseCheckDetailsMapper.deletePurchaseCheckDetailsByIds(ids);
    }

    /**
     * 删除新购验收任务详细信息
     * 
     * @param id 新购验收任务详细主键
     * @return 结果
     */
    @Override
    public int deletePurchaseCheckDetailsById(Long id) {
        return purchaseCheckDetailsMapper.deletePurchaseCheckDetailsById(id);
    }
}
