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

import java.util.List;

import com.bonus.sgzb.base.api.domain.TmTask;
import com.bonus.sgzb.material.domain.PurchaseCheckDetails;
import com.bonus.sgzb.material.domain.PurchaseMacodeInfo;
import com.bonus.sgzb.material.mapper.PurchaseCheckDetailsMapper;
import com.bonus.sgzb.material.mapper.TaskMapper;
import com.bonus.sgzb.material.service.IPurchaseCheckDetailsService;
import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.material.service.IPurchaseMacodeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 新购验收任务详细purchase_check_detailsService业务层处理
 *
 * @author bonus
 * @date 2023-12-10
 */
@Service
public class PurchaseCheckDetailsServiceImpl implements IPurchaseCheckDetailsService {
    @Resource
    private PurchaseCheckDetailsMapper purchaseCheckDetailsMapper;

    @Resource
    private TaskMapper taskMapper;

    @Resource
    private IPurchaseMacodeInfoService macodeInfoService;
    /**
     * 查询新购验收任务详细purchase_check_details
     *
     * @param taskId 新购验收任务详细purchase_check_details主键
     * @return 新购验收任务详细purchase_check_details
     */
    @Override
    public PurchaseCheckDetails selectPurchaseCheckDetailsByTaskId(Long taskId) {
        return purchaseCheckDetailsMapper.selectPurchaseCheckDetailsByTaskId(taskId);
    }

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

    /**
     * 新增新购验收任务详细purchase_check_details
     *
     * @param purchaseCheckDetailsList 新购验收任务详细purchase_check_details
     * @return 结果
     */
    @Override
    public int insertPurchaseCheckDetails(List<PurchaseCheckDetails> purchaseCheckDetailsList) {
//        purchaseCheckDetails.setCreateTime(DateUtils.getNowDate());
//        return purchaseCheckDetailsMapper.insertPurchaseCheckDetails(purchaseCheckDetails);
        return purchaseCheckDetailsMapper.insertPurchaseCheckDetailsList(purchaseCheckDetailsList);
    }

    /**
     * 修改新购验收任务详细purchase_check_details
     *
     * @param purchaseCheckDetailsList 新购验收任务详细purchase_check_details
     * @return 结果
     */
    @Override
    public int updatePurchaseCheckDetails(List<PurchaseCheckDetails> purchaseCheckDetailsList) {
        Long taskId = 0L;
        for (PurchaseCheckDetails purchaseCheckDetails : purchaseCheckDetailsList) {
            taskId = purchaseCheckDetails.getTaskId();
            if ("通过".equals(purchaseCheckDetails.getCheckResult())) {
                purchaseCheckDetails.setStatus(1);
                // 通过之后往编码管理表里回填数据
                PurchaseMacodeInfo macodeInfo = new PurchaseMacodeInfo();
                macodeInfo.setTaskId(purchaseCheckDetails.getTypeId());
                macodeInfo.setTaskId(taskId);
                macodeInfoService.insertPurchaseMacodeInfo(macodeInfo);
            } else if ("不通过".equals(purchaseCheckDetails.getCheckResult())) {
                purchaseCheckDetails.setStatus(0);
            } else {
                purchaseCheckDetails.setStatus(2);
            }
            purchaseCheckDetails.setUpdateTime(DateUtils.getNowDate());
            purchaseCheckDetailsMapper.updatePurchaseCheckDetails(purchaseCheckDetails);
        }
        // 当全部为已验收,修改任务验收状态
        int count = purchaseCheckDetailsMapper.selectPurchaseCheckDetailsStatus(taskId);
        if (count <= 0) {
            TmTask task = new TmTask();
            task.setTaskId(taskId);
            task.setTaskStatus(26);
            taskMapper.updateTmTask(task);
        }
        return 1;
    }

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

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