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



import com.bonus.common.biz.enums.PurchaseTaskStatusEnum;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.basic.mapper.BmFileInfoMapper;
import com.bonus.material.part.domain.*;
import com.bonus.material.part.mapper.PartAcceptMapper;

import com.bonus.material.part.mapper.PartArrivedMapper;
import com.bonus.material.part.service.IPartAcceptService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 配件新购管理Service业务层处理
 * 
 * @author hongchao
 * @date 2025-01-08
 */
@Service
public class PartAcceptServiceImpl implements IPartAcceptService
{
    @Resource
    private PartAcceptMapper partAcceptMapper;

    @Resource
    BmFileInfoMapper bmFileInfoMapper;

    @Resource
    private PartArrivedMapper partArrivedMapper;

    /**
     * 查询配件类型三级树
     * @param partTypeDto
     */
    @Override
    public AjaxResult getTypeTree(PartTypeDto partTypeDto) {
        List<PartTypeDto> typeList = partAcceptMapper.getTypeTree(partTypeDto);
        return AjaxResult.success(typeList);
    }

    /**
     * 入库
     * @param partTypeCheckDetails
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult partTypeWarehouse(List<PartTypeCheckDetails> partTypeCheckDetails) {
        if (partTypeCheckDetails.size() == 0){
            return AjaxResult.error("请选择要操作的记录");
        }
        for (PartTypeCheckDetails partTypeCheckDetail : partTypeCheckDetails) {
            int count =   partAcceptMapper.partTypeWarehouse(partTypeCheckDetail);
            int countTwo =   partAcceptMapper.updatePartTypeStorage(partTypeCheckDetail);

            if((count+countTwo) != 2){
                return AjaxResult.error("操作失败");
            }
        }
        PartTypeQueryDto partTypeQueryDto = new PartTypeQueryDto();
        Long taskId = partTypeCheckDetails.get(0).getTaskId();
        partTypeQueryDto.setTaskId(taskId);
        List<PartTypeCheckDetails> partTypeCheckDetailsList = partArrivedMapper.selectPartTypeCheckDetailsListByQueryDto(partTypeQueryDto);
        // 将集合中的status取出
        List<Integer> statusList = partTypeCheckDetailsList.stream()
                .map(PartTypeCheckDetails::getStatus)
                .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(statusList)) {
            int publishedStatus = PurchaseTaskStatusEnum.TO_CHECK.getStatus();
            int inProgressStatus = PurchaseTaskStatusEnum.TO_STORE.getStatus();
            int inStoreStatus = PurchaseTaskStatusEnum.IN_STORE.getStatus();
            int allRejectStatus = PurchaseTaskStatusEnum.TO_CHECK_AFTER_REJECT.getStatus();
            boolean notContainsNoFinished =!statusList.contains(publishedStatus);
            boolean containToStore = statusList.contains(inProgressStatus);
            boolean containReject = statusList.contains(allRejectStatus);
            boolean allStore = statusList.stream()
                    .allMatch(status -> status.equals(inStoreStatus));
            if (notContainsNoFinished && allStore) {
                partArrivedMapper.updateTaskStatus(taskId, PurchaseTaskStatusEnum.IN_STORE.getStatus());
            } else if (notContainsNoFinished && containToStore) {
                partArrivedMapper.updateTaskStatus(taskId, PurchaseTaskStatusEnum.TASK_IN_PROGRESS.getStatus());
            } else if (!containToStore && containReject) {
                partArrivedMapper.updateTaskStatus(taskId, PurchaseTaskStatusEnum.IN_STORE.getStatus());
            }
        }
        return AjaxResult.success();
    }

    /**
     * 驳回
     * @param partTypeCheckDetails
     * @return
     */
    @Override
    public AjaxResult partTypeReject(List<PartTypeCheckDetails> partTypeCheckDetails) {
        if (partTypeCheckDetails.size() == 0){
            return AjaxResult.error("请选择要操作的记录");
        }
        for (PartTypeCheckDetails partTypeCheckDetail : partTypeCheckDetails) {
            int count =   partAcceptMapper.partTypeReject(partTypeCheckDetail);

            if(count != 1){
                return AjaxResult.error("操作失败");
            }
        }
        int countThree = partAcceptMapper.selectStatus(partTypeCheckDetails.get(0).getTaskId());
        if(countThree == 0) {
            partAcceptMapper.updateStatus(partTypeCheckDetails.get(0).getTaskId());
        }
        return AjaxResult.success();
    }


    /**
     * 外层入库
     * @param partTypeCheckInfo
     * @return
     */
    @Override
    public AjaxResult passAll(List<PartTypeCheckInfo> partTypeCheckInfo) {
        if (partTypeCheckInfo.size() == 0){
            return AjaxResult.error("请选择要操作的记录");
        }
        for (PartTypeCheckInfo vo : partTypeCheckInfo) {
            int count = partAcceptMapper.passAll(vo.getTaskId());
            if(count == 0){
                return AjaxResult.error("操作失败");
            }
            partAcceptMapper.updateStatus(vo.getTaskId());
        }
        return AjaxResult.success();
    }

    /**
     * 外层驳回
     * @param partTypeCheckInfo
     * @return
     */
    @Override
    public AjaxResult rejectAll(List<PartTypeCheckInfo> partTypeCheckInfo) {
        if (partTypeCheckInfo.size() == 0){
            return AjaxResult.error("请选择要操作的记录");
        }
        for (PartTypeCheckInfo vo : partTypeCheckInfo) {
            int count = partAcceptMapper.rejectAll(vo.getTaskId());
            if(count == 0){
                return AjaxResult.error("操作失败");
            }
            partAcceptMapper.updateStatus(vo.getTaskId());
        }
        return AjaxResult.success();
    }
}
