package com.sw.common.accessory.server.service.impl;

import com.sw.common.accessory.api.feignCilent.YbFeignClients;
import com.sw.common.accessory.api.model.EquipmentDto;
import com.sw.common.accessory.server.dto.AccessoryPartBo;
import com.sw.common.accessory.server.dto.AccessoryPartDto;
import com.sw.common.accessory.server.entity.AccessoryPart;
import com.sw.common.accessory.server.entity.AccessoryPartDetails;
import com.sw.common.accessory.server.mapper.AccessoryPartMapper;
import com.sw.common.accessory.server.service.AccessoryPartDetailsService;
import com.sw.common.accessory.server.service.AccessoryPartService;
import com.sw.common.core.context.BaseContextHandler;
import com.sw.common.core.response.Result;
import com.sw.common.mybatisplus.base.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
@Service
public class AccessoryPartServiceImpl extends BaseServiceImpl<AccessoryPartMapper, AccessoryPart> implements AccessoryPartService {
    @Autowired
    private AccessoryPartMapper accessoryPartMapper;
    @Autowired
    private AccessoryPartDetailsService accessoryPartDetailsService;
    @Autowired
    private YbFeignClients ybFeignClients;

    @Override
    @Transactional
    public Boolean create(AccessoryPartBo bo) {
        if(ObjectUtils.isEmpty(bo)){
            return false;
        }
        if(!ObjectUtils.isEmpty(bo.getPartDtos())){
            bo.getPartDtos().stream().forEach(entity->{
                AccessoryPart accessoryPart = new AccessoryPart();
                BeanUtils.copyProperties(bo,accessoryPart,"id");
                accessoryPart.setTotalNumber(entity.getCount());
                accessoryPart.setUsableNumber(entity.getCount());
                accessoryPart.setUsedNumber(0);
                accessoryPart.setMaterialId(entity.getMaterialId());
                accessoryPart.setMaterialCode(entity.getMaterialCode());
                accessoryPart.setMaterialName(entity.getMaterialName());
                accessoryPart.setPartCode(entity.getPartCode());
                accessoryPart.setPartName(entity.getPartName());
                accessoryPart.setIsUse(true);
                accessoryPart.setCreateBy(BaseContextHandler.getUsername());
                accessoryPart.setCreateTime(new Date());
                accessoryPartMapper.insert(accessoryPart);
                Result<List<EquipmentDto>> equipments = ybFeignClients.findByDeviceId(bo.getDeviceTypeId());
                if(equipments.getCode().equals(200)){
                    equipments.getData().stream().forEach(equipment->{
                           AccessoryPartDetails accessoryPartDtails = new AccessoryPartDetails();
                           accessoryPartDtails.setPartCode(entity.getPartCode());
                           accessoryPartDtails.setPartName(entity.getPartName());
                           accessoryPartDtails.setMaterialCode(entity.getMaterialCode());
                           accessoryPartDtails.setMaterialId(entity.getMaterialId());
                           accessoryPartDtails.setMaterialName(entity.getMaterialName());
                           accessoryPartDtails.setDeviceModelId(Integer.valueOf(String.valueOf(equipment.getId())));
                           accessoryPartDtails.setDeviceModelCode(equipment.getCode());
                           accessoryPartDtails.setDeviceModelName(equipment.getName());
                           accessoryPartDtails.setDeviceTypeId(bo.getDeviceTypeId());
                           accessoryPartDtails.setDeviceTypeCode(bo.getDeviceTypeCode());
                           accessoryPartDtails.setDeviceTypeName(bo.getDeviceTypeName());
                           accessoryPartDtails.setCreateBy(BaseContextHandler.getUsername());
                           accessoryPartDtails.setCreateTime(new Date());
                           accessoryPartDtails.setTotalNumber(entity.getCount());
                           accessoryPartDtails.setUsableNumber(entity.getCount());
                           accessoryPartDtails.setUsedNumber(0);
                           accessoryPartDtails.setIsUse(true);
                           accessoryPartDetailsService.saveOrUpdate(accessoryPartDtails);
                    });
                }

            });
        }else {
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean update(AccessoryPartDto dto) {
        if(ObjectUtils.isEmpty(dto)){
            return false;
        }
        try{
            AccessoryPart accessoryPart = new AccessoryPart();
            BeanUtils.copyProperties(dto,accessoryPart);
            accessoryPart.setUsableNumber(accessoryPart.getTotalNumber(),accessoryPart.getUsedNumber());
            accessoryPart.setUpdateBy(BaseContextHandler.getUsername());
            accessoryPart.setUpdateTime(new Date());
            accessoryPartMapper.updateById(accessoryPart);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<AccessoryPart> getByMaterialCode(String materialCode) {
        return accessoryPartMapper.getByMaterialCode(materialCode);
    }

    @Override
    public List<AccessoryPart> getByPartCode(String partCode) {
        return accessoryPartMapper.getByPartCode(partCode);
    }

    @Override
    public AccessoryPart getByDeviceCodeAndMaterialCode(String deviceCode,String materialCode) {
        return accessoryPartMapper.getByDeviceCodeAndMaterialCode(deviceCode,materialCode);
    }

    @Override
    public AccessoryPart getByCode(String deviceTypeCode,String deviceCode,String materialCode, String partCode) {
        return accessoryPartMapper.getByCode(deviceTypeCode,deviceCode,materialCode,partCode);
    }

    @Override
    public AccessoryPart getByTypeCode(String deviceTypeCode,String materialCode, String partCode) {
        return accessoryPartMapper.getByTypeCode(deviceTypeCode,materialCode,partCode);
    }
}
