package com.sw.mes.fb.accessory.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.base.factory.product.entity.FbAccessoryMaterial;
import com.sw.mes.base.factory.product.service.FbAccessoryMaterialService;
import com.sw.mes.fb.accessory.dao.FbEquipmentAccessoryDao;
import com.sw.mes.fb.accessory.domain.OrderUpdate;
import com.sw.mes.fb.accessory.dto.*;
import com.sw.mes.fb.accessory.entity.FbAccessoryOrder;
import com.sw.mes.fb.accessory.entity.FbEquipmentAccessory;
import com.sw.mes.fb.accessory.service.FbAccessoryOrderService;
import com.sw.mes.fb.accessory.service.FbEquipmentAccessoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional(rollbackFor = Throwable.class)
public class FbEquipmentAccessoryServiceImpl extends BaseServiceImpl<FbEquipmentAccessory> implements FbEquipmentAccessoryService {
    FbEquipmentAccessoryDao dao;
	@Autowired
    public void setDao(FbEquipmentAccessoryDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private FbAccessoryMaterialService accessoryMaterialService;

    @Autowired
    private FbAccessoryOrderService accessoryOrderService;

    @Override
    public void createMonitor(FbEquipmentAccessoryCreatDto createDto) {
        FbEquipmentAccessory accessory = findByGoodsCodeAndDeleted(createDto.getGoodsCode(), 0);
        if (accessory != null){
            throw new RuntimeException("当前条码（唯一码）在辅料监控中已存在");
        }

        Equipment equipment = equipmentService.get(createDto.getEquipmentId());
        if (equipment == null){
            throw new RuntimeException("当前设备不存在");
        }

        FbAccessoryMaterial accessoryMaterial = accessoryMaterialService.get(createDto.getMaterialId());
        if (accessoryMaterial == null) {
            throw new RuntimeException("当前辅料模型不存在");
        }

        FbEquipmentAccessory equipmentAccessory = new FbEquipmentAccessory();
        equipmentAccessory.setEquipment(equipment);
        equipmentAccessory.setMaterial(accessoryMaterial);
        equipmentAccessory.setDeleted(0);
        equipmentAccessory.setFactory(createDto.getFactory());
        equipmentAccessory.setDeviceSeat(createDto.getDeviceSeat());
        equipmentAccessory.setGoodsCode(createDto.getGoodsCode());
        equipmentAccessory.setNum(createDto.getNum());
        equipmentAccessory.setUseTime(createDto.getUseTime());
        equipmentAccessory.setOperateDate(new Date());
        equipmentAccessory.setOperateUser(BaseContextHandler.getUsername());
        equipmentAccessory.setOperateUserId(BaseContextHandler.getUserID());
        equipmentAccessory.setUpdateDate(new Date());
        saveOrUpdate(equipmentAccessory);
    }

    public FbEquipmentAccessory findByGoodsCodeAndDeleted(String goodsCode, Integer deleted) {
        List<FbEquipmentAccessory> list = dao.findByGoodsCodeAndDeletedOrderByIdDesc(goodsCode, deleted);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Page<Map<String, Object>> newListByPage(NewAccessoryListByPageParamDto paramDto, Pageable pageable) {
        return dao.newListByPage(paramDto.getDeviceNo(),paramDto.getMaterialName(),paramDto.getMaterialCode(),paramDto.getGoodsCode(),paramDto.getFactory()
                                 ,paramDto.getOperateTimeStart(), paramDto.getOperateTimeEnd(), pageable);
    }

    /**
     * 可以获取实时的已使用寿命
     * @param id
     * @return
     */
    @Override
    public NewAccessoryDto getById(Long id) {
        Map<String,Object> map = dao.getById(id);
        return JSON.parseObject(JSON.toJSONString(map), new TypeReference<NewAccessoryDto>() {});
    }

    /**
     * 截断机滑轮
     * @param paramDto
     * @param pageable
     * @return
     */
    @Override
    public Page<Map<String, Object>> jdjhl(FbAccessoryReportParamDto paramDto, Pageable pageable) {
        return dao.jdjhl(paramDto.getGoodsCode(), paramDto.getMaterialCode(), paramDto.getDeviceNo(), paramDto.getStartUpDate(), paramDto.getEndUpDate(), paramDto.getStartDownDate(), paramDto.getEndDownDate(), pageable);
    }

    /**
     * 截断机滑轮详情
     * @param paramDto
     * @return
     */
    @Override
    public List<Map<String, Object>> jdjhlDetail(FbAccessoryReportDetailParamDto paramDto) {
        return dao.jdjhlDetail(paramDto.getDeviceNo(),paramDto.getUpDate(),paramDto.getDownDate());
    }

    /**
     * 截断机切割线
     * @param paramDto
     * @param pageable
     * @return
     */
    @Override
    public Page<Map<String, Object>> jdjqgx(FbAccessoryReportParamDto paramDto, Pageable pageable) {
        return dao.jdjqgx(paramDto.getGoodsCode(), paramDto.getMaterialCode(), paramDto.getDeviceNo(), paramDto.getStartUpDate(), paramDto.getEndUpDate(), paramDto.getStartDownDate(), paramDto.getEndDownDate(), pageable);
    }

    /**
     * 截断机切割线详情
     * @param paramDto
     * @return
     */
    @Override
    public List<Map<String, Object>> jdjqgxDetail(FbAccessoryReportDetailParamDto paramDto) {
        return dao.jdjqgxDetail(paramDto.getDeviceNo(),paramDto.getUpDate(),paramDto.getDownDate());
    }

    /**
     * 磨削机砂轮
     * @param paramDto
     * @param pageable
     * @return
     */
    @Override
    public Page<Map<String, Object>> mxjsl(FbAccessoryReportParamDto paramDto, Pageable pageable) {
        return dao.mxjsl(paramDto.getGoodsCode(), paramDto.getMaterialCode(), paramDto.getDeviceNo(), paramDto.getStartUpDate(), paramDto.getEndUpDate(), paramDto.getStartDownDate(), paramDto.getEndDownDate(), pageable);
    }

    /**
     * 磨削机砂轮详情
     * @param paramDto
     * @return
     */
    @Override
    public List<Map<String, Object>> mxjslDetail(FbAccessoryReportDetailParamDto paramDto) {
        return dao.mxjslDetail(paramDto.getDeviceNo(),paramDto.getUpDate(),paramDto.getDownDate());
    }

    /**
     * 磨削机修整砂轮
     * @param paramDto
     * @param pageable
     * @return
     */
    @Override
    public Page<Map<String, Object>> mxjxzsl(FbAccessoryReportParamDto paramDto, Pageable pageable) {
        return dao.mxjxzsl(paramDto.getGoodsCode(), paramDto.getMaterialCode(), paramDto.getDeviceNo(), paramDto.getStartUpDate(), paramDto.getEndUpDate(), paramDto.getStartDownDate(), paramDto.getEndDownDate(), pageable);
    }

    /**
     * 磨削机修整砂轮详情
     * @param paramDto
     * @return
     */
    @Override
    public List<Map<String, Object>> mxjxzslDetail(FbAccessoryReportDetailParamDto paramDto) {
        return dao.mxjxzslDetail(paramDto.getDeviceNo(),paramDto.getUpDate(),paramDto.getDownDate());
    }

    /**
     * 切方机滑轮
     * @param paramDto
     * @param pageable
     * @return
     */
    @Override
    public Page<Map<String, Object>> qfjhl(FbAccessoryReportParamDto paramDto, Pageable pageable) {
        return dao.qfjhl(paramDto.getGoodsCode(), paramDto.getMaterialCode(), paramDto.getDeviceNo(), paramDto.getStartUpDate(), paramDto.getEndUpDate(), paramDto.getStartDownDate(), paramDto.getEndDownDate(), pageable);
    }

    /**
     * 切方机滑轮详情
     * @param paramDto
     * @return
     */
    @Override
    public List<Map<String, Object>> qfjhlDetail(FbAccessoryReportDetailParamDto paramDto) {
        return dao.qfjhlDetail(paramDto.getDeviceNo(),paramDto.getUpDate(),paramDto.getDownDate());
    }

    /**
     * 切方机切割线
     * @param paramDto
     * @param pageable
     * @return
     */
    @Override
    public Page<Map<String, Object>> qfjqgx(FbAccessoryReportParamDto paramDto, Pageable pageable) {
        return dao.qfjqgx(paramDto.getGoodsCode(), paramDto.getMaterialCode(), paramDto.getDeviceNo(), paramDto.getStartUpDate(), paramDto.getEndUpDate(), paramDto.getStartDownDate(), paramDto.getEndDownDate(), pageable);
    }

    /**
     * 切方机切割线详情
     * @param paramDto
     * @return
     */
    @Override
    public List<Map<String, Object>> qfjqgxDetail(FbAccessoryReportDetailParamDto paramDto) {
        return dao.qfjqgxDetail(paramDto.getDeviceNo(),paramDto.getUpDate(),paramDto.getDownDate());
    }

    @Override
    public void updateMonitor(FbEquipmentAccessoryUpdateDto updateDto) {
        FbEquipmentAccessory accessory = findByGoodsCodeAndDeleted(updateDto.getGoodsCode(), 0);
        if (accessory != null){
            throw new RuntimeException("当前条码（唯一码）在辅料监控中已存在");
        }

        FbEquipmentAccessory equipmentAccessory = get(updateDto.getId());
        if (equipmentAccessory == null) {
            throw new RuntimeException("该设备监控不存在");
        }
        NewAccessoryDto newAccessoryDto = getById(updateDto.getId());
        equipmentAccessory.setUseTime(newAccessoryDto.getUseTime());  //赋予实时已使用寿命

        FbAccessoryMaterial accessoryMaterial = accessoryMaterialService.get(updateDto.getMaterialId());
        if (accessoryMaterial == null) {
            throw new RuntimeException("更换上的物料模型不存在");
        }

        EquipmentClassTeam equipmentClassTeam = new EquipmentClassTeam();

        //获取当前设备班组班次
        equipmentClassTeam.setDownClassNo(updateDto.getClassNo());
        equipmentClassTeam.setDownTeamGroup(updateDto.getTeamGroup());
        //获取上次更换信息的设备班次班组
        FbAccessoryOrder oldOrder = accessoryOrderService.findByGoodsCodeAfter(equipmentAccessory.getGoodsCode());
        if (oldOrder != null){
            equipmentClassTeam.setUpClassNo(oldOrder.getDownClassNo());
            equipmentClassTeam.setUpTeamGroup(oldOrder.getDownTeamGroup());
        }

        //构造更换前后信息
        OrderUpdate orderUpdate = new OrderUpdate(equipmentAccessory, accessoryMaterial, updateDto, equipmentClassTeam);
        FbAccessoryOrder order = accessoryOrderService.generateMonitorOrder(orderUpdate);

        equipmentAccessory.setOperateUserId(BaseContextHandler.getUserID());
        equipmentAccessory.setOperateUser(BaseContextHandler.getUsername());
        equipmentAccessory.setOperateDate(updateDto.getOperateTime());
        equipmentAccessory.setUpdateDate(new Date());
        equipmentAccessory.setGoodsCode(updateDto.getGoodsCode());
        equipmentAccessory.setUseTime(updateDto.getUseTime());
        equipmentAccessory.setFactory(updateDto.getFactory());
        equipmentAccessory.setMaterial(accessoryMaterial);

        saveOrUpdate(equipmentAccessory);
        accessoryOrderService.saveOrUpdate(order);
    }
}
