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

import java.util.List;

import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.material.domain.PurchaseCheckDetails;
import com.bonus.sgzb.material.domain.TmTask;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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 List<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) {
        return purchaseCheckDetailsMapper.insertPurchaseCheckDetailsList(purchaseCheckDetailsList);
    }

    /**
     * 验收审核
     * 通过/不通过
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePurchaseCheckDetails(List<PurchaseCheckDetails> purchaseCheckDetailsList) {
        Long taskId = 0L;
        for (PurchaseCheckDetails purchaseCheckDetails : purchaseCheckDetailsList) {
            taskId = purchaseCheckDetails.getTaskId();
            if ("通过".equals(purchaseCheckDetails.getCheckResult())) {
                purchaseCheckDetails.setStatus(1);
            } else if ("不通过".equals(purchaseCheckDetails.getCheckResult())) {
                purchaseCheckDetails.setStatus(3);
            } else {
                purchaseCheckDetails.setStatus(2);
            }
            purchaseCheckDetails.setUpdateTime(DateUtils.getNowDate());
            purchaseCheckDetailsMapper.updatePurchaseCheckDetails(purchaseCheckDetails);
        }
        //判断是否全部已操作（不通过或通过）
        int count=purchaseCheckDetailsMapper.isOperateAll(taskId);
        if (count<=0){
            //是否为全部不通过
            int count1 = purchaseCheckDetailsMapper.selectPurchaseCheckDetailsStatus(taskId);
            if (count1 <= 0) {
                //是则将任务状态改为验收不通过
                TmTask task = new TmTask();
                task.setTaskId(taskId);
                task.setTaskStatus(27);
                task.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserid()));
                task.setUpdateTime(DateUtils.getNowDate());
                taskMapper.updateTmTask(task);
            }else {
                // 当全部操作,修改任务为验收状态
                TmTask task = new TmTask();
                task.setTaskId(taskId);
                task.setTaskStatus(26);
                task.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserid()));
                task.setUpdateTime(DateUtils.getNowDate());
                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 id 新购验收任务详细purchase_check_details主键
     * @return 结果
     */
    @Override
    public int deletePurchaseCheckDetailsByTaskId(Long id) {
        return purchaseCheckDetailsMapper.deletePurchaseCheckDetailsByTaskId(id);
    }
}
