package com.yeejoin.amos.boot.module.jcs.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.biz.common.utils.DateUtils;
import com.yeejoin.amos.boot.module.common.api.dto.FirefightersJacketDto;
import com.yeejoin.amos.boot.module.common.api.dto.FirestationJacketDto;
import com.yeejoin.amos.boot.module.common.api.entity.FirefightersJacket;
import com.yeejoin.amos.boot.module.common.api.entity.FirestationJacket;
import com.yeejoin.amos.boot.module.common.api.feign.EquipFeignClient;
import com.yeejoin.amos.boot.module.common.api.mapper.FirefightersJacketMapper;
import com.yeejoin.amos.boot.module.common.api.mapper.FirestationJacketMapper;
import com.yeejoin.amos.boot.module.jcs.api.dto.EquipSpecificDto;
import com.yeejoin.amos.boot.module.jcs.api.dto.EquipmentOnCarDto;
import com.yeejoin.amos.boot.module.jcs.api.enums.EquipTypeEnum;
import com.yeejoin.amos.boot.module.jcs.api.service.IFirefightersJacketService;
import com.yeejoin.amos.boot.module.jcs.api.service.IFirestationJacketService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 消防人员配装记录 服务实现类
 *
 * @author tb
 * @date 2021-06-07
 */
@Service
public class FirestationJacketServiceImpl extends BaseService<FirestationJacketDto, FirestationJacket, FirestationJacketMapper> implements IFirestationJacketService {

    @Autowired
    private EquipFeignClient equipFeignClient;

    @Override
    public ResponseModel<Page<Object>> getAirEquipSpecificPage(EquipSpecificDto equipSpecificDto, int current, int size) {
        Page<Object> page = new Page<Object>(current, size);
        equipSpecificDto.setPage(page);
        ResponseModel<Page<Object>> airEquipSpecificPage = equipFeignClient.getAirEquipSpecificPage(equipSpecificDto);
        return airEquipSpecificPage;
    }

    @Override
    public boolean saveOrUpdateBatch(Long firefightersId, List<EquipmentOnCarDto> equipmentOnCarDtos) {
        List<Long> ids = equipmentOnCarDtos.stream().map(EquipmentOnCarDto::getStockDetailId).collect(Collectors.toList());
        List<Map<String, Number>> stocks = new ArrayList<>();
        for (EquipmentOnCarDto dto : equipmentOnCarDtos) {
            stocks.add(new HashMap<String, Number>() {{
                this.put("id", dto.getStockDetailId());
                this.put("amount", dto.getAmount());
            }});
        }
        List<Object> result = equipFeignClient.stockBindEquip(stocks).getResult();
        if (result.size() > 0) {
            List<FirestationJacket> firefightersJackets = new ArrayList<>();
            equipmentOnCarDtos.forEach(x -> {
                FirestationJacket firefightersJacket = new FirestationJacket();
                BeanUtils.copyProperties(x, firefightersJacket);
                firefightersJacket.setEquipmentDetailId(x.getEquipmentSpecificId());
                firefightersJacket.setEquipmentDetailName(x.getEquipmentSpecificName());
                firefightersJacket.setEquipmentTypeName(x.getEquipDefinitionName());
                firefightersJacket.setAllotmentTime(DateUtils.getDateNow());
                firefightersJacket.setFirefightersId(firefightersId);
                firefightersJackets.add(firefightersJacket);
            });
            saveOrUpdateBatch(firefightersJackets);
            return true;
        }
        return false;
    }

    @Override
    public boolean update(String type, FirestationJacket firestationJacket) {
        ResponseModel<Object> objectResponseModel = null;
        Map<String, Object> map = new HashMap<>();
        map.put("firefightersId", firestationJacket.getFirefightersId());
        map.put("stockDetailId", firestationJacket.getStockDetailId());
        map.put("amount", firestationJacket.getAmount());
        if (EquipTypeEnum.报废.getKey().equals(type)) {
            objectResponseModel = equipFeignClient.scrapEquip(map);
        } else if (EquipTypeEnum.回库.getKey().equals(type)) {
            objectResponseModel = equipFeignClient.stockEquip(map);
        }
        if (ValidationUtil.isEmpty(objectResponseModel)){
            return false;
        }
        update(new UpdateWrapper<FirestationJacket>().eq("sequence_nbr", firestationJacket.getSequenceNbr()).set("is_delete", 1));
        return true;
    }


    @Override
    public ResponseModel<Object> getEquipByStockDetailId(Long stockDetailId) {
        return equipFeignClient.getAirEquipSpecificDetail(stockDetailId);
    }
}
