package com.fjhx.mes.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.fjhx.common.utils.Assert;
import com.fjhx.file.entity.FileInfoVo;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.iot.entity.tda.po.TdaDevice;
import com.fjhx.iot.service.tda.TdaDeviceService;
import com.fjhx.mes.entity.production.po.ProductionOrderDetail;
import com.fjhx.mes.entity.work.dto.WorkOrderDto;
import com.fjhx.mes.entity.work.po.WorkOrder;
import com.fjhx.mes.service.production.ProduceOrderDetailService;
import com.fjhx.mes.service.work.WorkOrderService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class DeviceOpenServiceImpl implements DeviceOpenService {

    @Autowired
    private TdaDeviceService tdaDeviceService;
    @Autowired
    private ProduceOrderDetailService produceOrderDetailService;
    @Autowired
    private WorkOrderService workOrderService;

    /**
     * 开始生产
     */
    @DSTransactional
    @Override
    public Map<String, Object> startProduction(WorkOrderDto dto) {
        SecurityUtils.setTenantId("000000");

        //根据设备标识获取任务信息
        String deviceIdent = dto.getDeviceIdent();
        TdaDevice tdaDevice = tdaDeviceService.getOne(q -> q.eq(TdaDevice::getDeviceCode, deviceIdent));
        Assert.notEmpty(tdaDevice, "查询不到设备信息");
        ProductionOrderDetail productionOrderDetail = produceOrderDetailService.getById(tdaDevice.getProdTaskId());
        Assert.notEmpty(productionOrderDetail, "查询不到任务信息");

        //获取文件信息
        Map<Long, List<FileInfoVo>> fileMap = ObsFileUtil.getFileMap(Arrays.asList(productionOrderDetail.getContractDetailId()), 2);
        List<FileInfoVo> fileInfoList = fileMap.getOrDefault(productionOrderDetail.getContractDetailId(), new ArrayList<>());
        if (ObjectUtil.isEmpty(fileInfoList)) {
            throw new ServiceException("查询不到生产文件信息！");
        }

        //创建工单
        dto.setContractId(productionOrderDetail.getContractId());
        dto.setContractDetailsId(productionOrderDetail.getContractDetailId());
        dto.setProdTaskId(productionOrderDetail.getId());
        dto.setProdOrderId(productionOrderDetail.getProduceOrderId());
        dto.setProductId(productionOrderDetail.getProductId());
        dto.setQuantity(BigDecimal.ONE);
        dto.setStatus(1);//进行中
        dto.setStaTime(new Date());
        workOrderService.save(dto);

        SecurityUtils.clearTenantId();

        Map<String, Object> data = new HashMap<>();
        data.put("artNum", dto.getId());//工单号
        data.put("fileUrl", fileInfoList.get(0).getFileUrl());//生产文件
        return data;
    }

    /**
     * 结束生产
     */
    @Override
    public void endProduction(WorkOrderDto dto) {
        SecurityUtils.setTenantId("000000");

        Long artNum = dto.getArtNum();
        Assert.notEmpty(artNum, "工单号不能为空!");
        WorkOrder byId = workOrderService.getById(artNum);
        Assert.notEmpty(byId, "查询不到工单信息!");

        dto.setId(artNum);
        dto.setStatus(2);//已完成
        dto.setEndTime(new Date());
        workOrderService.updateById(dto);

        SecurityUtils.clearTenantId();
    }
}
