package org.jeecg.modules.workOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.apply.entity.ApplyRecord;
import org.jeecg.modules.apply.mapper.ApplyRecordMapper;
import org.jeecg.modules.workOrder.entity.WorkOrder;
import org.jeecg.modules.workOrder.entity.WorkPermitComplete;
import org.jeecg.modules.workOrder.mapper.WorkOrderMapper;
import org.jeecg.modules.workOrder.mapper.WorkPermitCompleteMapper;
import org.jeecg.modules.workOrder.service.IWorkPermitCompleteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 请销点记录
 * @Author: jeecg-boot
 * @Date:   2025-02-14
 * @Version: V1.0
 */
@Service
public class WorkPermitCompleteServiceImpl extends ServiceImpl<WorkPermitCompleteMapper, WorkPermitComplete> implements IWorkPermitCompleteService {

    @Autowired
    private ApplyRecordMapper applyRecordMapper;
    @Autowired
    private WorkOrderMapper workOrderMapper;
    /**
     * 请销点保存方法
     * @param record 实体对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addPermit(WorkPermitComplete record) {

        int num = workOrderMapper.selectByWorkOrderId(record.getWorkOrder());
        List<ApplyRecord> list = applyRecordMapper.selectList(new LambdaQueryWrapper<ApplyRecord>()
                .eq(ApplyRecord::getWorkOrder, record.getWorkOrder()).orderByDesc(ApplyRecord::getCreateTime).last("limit 2"));
        if(num > 0 && list.isEmpty()) {
            return 0; // 应该申请物料和工具却没有申请的，也要拦截
        }
        if (list.stream().anyMatch(item -> item.getStatus() != 1)) {
            // 请点时，如果物资申请没通过
            return 0;
        }

        if (super.save(record)) {
            // 请点记录创建成功时更新工单表的标志位
            workOrderMapper.update(null, new LambdaUpdateWrapper<WorkOrder>()
                    .eq(WorkOrder::getId, record.getWorkOrder())
                    .set(WorkOrder::getPointRequestStatus, 1)
                    .set(WorkOrder::getStatus, 5));
        }else {
            return 2;
        }
        // 请点保存成功
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateComple(WorkPermitComplete permitComplete) {
        // 除开创建请销点记录那一步，后面所有设计修改请销点记录该条数据的操作，请点站值班员字段都必须不为空
        if(StringUtils.isBlank(permitComplete.getPermitOfficer())){
            return 0;
        }
        boolean flag = super.updateById(permitComplete);
        if (flag && StringUtils.isNotBlank(permitComplete.getCompleOfficer())) {
            // 提交销点站值班员信息时认为是销点成功，销点成功时更新工单表的标志位
            workOrderMapper.update(null, new LambdaUpdateWrapper<WorkOrder>()
                    .eq(WorkOrder::getId, permitComplete.getWorkOrder())
                    .set(WorkOrder::getPointRequestStatus, 2));
        }else if (flag){
            // 如果未提交销点站值班员，即使已经提交了销点申请人同样返回“编辑成功”
            return 2;
        }else {
            //如果请销点记录更新失败，返回“失败”
            return 3;
        }
        // 销点保功
        return 1;
    }

    /**
     * 查询工单是否完成请点
     * @param workOrderId
     * @return
     */
    @Override
    public Boolean getCheckStatus(String workOrderId) {
        long total = baseMapper.selectCount(new LambdaQueryWrapper<WorkPermitComplete>().eq(WorkPermitComplete::getWorkOrder,workOrderId));
        if(total==0){
            return false;
        }
        long permit = baseMapper.selectCount(new LambdaQueryWrapper<WorkPermitComplete>()
                .eq(WorkPermitComplete::getWorkOrder,workOrderId).isNotNull(WorkPermitComplete::getPermitOfficer));
        return total == permit;
    }

}
