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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.enums.EnumValues;
import com.sw.mes.yb.accessory.dao.AccessoryReturnFeedbackDao;
import com.sw.mes.yb.accessory.dto.AccessoryReturnFeedbackDto;
import com.sw.mes.yb.accessory.entity.*;
import com.sw.mes.yb.accessory.service.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class AccessoryReturnFeedbackServiceImpl extends BaseServiceImpl<AccessoryReturnFeedback> implements AccessoryReturnFeedbackService {
    AccessoryReturnFeedbackDao dao;
	@Autowired
    public void setDao(AccessoryReturnFeedbackDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    private AccessoryReturnService accessoryReturnService;

	@Autowired
    private EquipmentAccessoryService equipmentAccessoryService;

	@Autowired
    private AccessoryMaterialOutService accessoryMaterialOutService;

	@Autowired
    private AccessoryApplyService accessoryApplyService;

	@Autowired
    private AccessoryReturnFeedbackService accessoryReturnFeedbackService;

	@Autowired
    private ModelMapper modelMapper;

    @Override
    @Transactional
    public void saveFeedback(AccessoryReturnFeedback accessoryReturnFeedback) {
        AccessoryReturn accessoryReturn = accessoryReturnService.findByOrderNum(accessoryReturnFeedback.getOrderNum());
        if (accessoryReturn == null){
            throw new RuntimeException("退料单号"+accessoryReturnFeedback.getOrderNum()+"有误！");
        }
        if (!EnumValues.AccessoryReturnStatus.NOTCONFIRMED.getKey().equals(accessoryReturn.getState())){
            throw new RuntimeException(accessoryReturnFeedback.getOrderNum()+"请不要重复处理！");
        }

        if(accessoryReturnFeedback.getType() == 1){
            accessoryReturn.setState(EnumValues.AccessoryReturnStatus.CONFIRMED.getKey());
            accessoryReturn.setStateDate(new Date());
            accessoryReturn.setAccessoryReturnFeedback(accessoryReturnFeedback);
        }else {
            accessoryReturn.setState(EnumValues.AccessoryReturnStatus.REJECTED.getKey());
            accessoryReturn.setStateDate(new Date());
            accessoryReturn.setAccessoryReturnFeedback(accessoryReturnFeedback);
        }
        accessoryReturnFeedback.setId(null);
        saveOrUpdate(accessoryReturnFeedback);
        accessoryReturnService.saveOrUpdate(accessoryReturn);
    }

    @Override
    @Transactional
    public void batchSaveFeedback(List<AccessoryReturnFeedbackDto> dtos) {
        for (AccessoryReturnFeedbackDto dto : dtos) {
            AccessoryReturnFeedback accessoryReturnFeedback = fromDto(dto);
            accessoryReturnFeedbackService.saveFeedback(accessoryReturnFeedback);
        }
    }

    public AccessoryReturnFeedback fromDto(AccessoryReturnFeedbackDto dto) {
        return modelMapper.map(dto, AccessoryReturnFeedback.class);
    }
}
