package com.njcn.csdevice.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.mapper.CsEquipmentTransferPOMapper;
import com.njcn.csdevice.mapper.CsLedgerMapper;
import com.njcn.csdevice.pojo.param.CsEquipmentTransferAddParm;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.po.CsEquipmentTransferPO;
import com.njcn.csdevice.pojo.po.CsLedger;
import com.njcn.csdevice.service.CsDeviceUserPOService;
import com.njcn.csdevice.service.CsEquipmentTransferPOService;
import com.njcn.csdevice.service.ICsLedgerService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;

/**
 * 
 * Description:
 * Date: 2023/5/17  15:40【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Deprecated
public class CsEquipmentTransferPOServiceImpl extends ServiceImpl<CsEquipmentTransferPOMapper, CsEquipmentTransferPO> implements CsEquipmentTransferPOService{
    private final CsDeviceUserPOService csDeviceUserPOService;

    private final ICsLedgerService csLedgerService;
    private final DicDataFeignClient dicDataFeignClient;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean add(CsEquipmentTransferAddParm csEquipmentTransferAddParm) {
        CsEquipmentTransferPO csEquipmentTransferPO = new CsEquipmentTransferPO();
        BeanUtils.copyProperties(csEquipmentTransferAddParm, csEquipmentTransferPO);
        csEquipmentTransferPO.setStatus(DataStateEnum.ENABLE.getCode());
        boolean save = this.save(csEquipmentTransferPO);

        log.info("插入成功"+JSON.toJSONString(csEquipmentTransferPO));

        return save;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean pass(List<String> ids) {
        List<DictData> data = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.APP_DEVICE_EVENT_TYPE.getCode()).getData();
        Map<String, String> collect1 = data.stream().collect(Collectors.toMap(DictData::getCode, DictData::getId));
        ids.forEach(id -> {
            CsEquipmentTransferPO csEquipmentTransferPO = this.getById(id);
            if(!Objects.isNull(csEquipmentTransferPO.getProjectId())){
                List<CsLedger> list = csLedgerService.lambdaQuery().eq(CsLedger::getPid, csEquipmentTransferPO.getProjectId()).eq(CsLedger::getState, 1).list();
                String collect = list.stream().map(CsLedger::getId).collect(Collectors.joining(","));
                csEquipmentTransferPO.setEquipmentIds(collect);
            }
            if(!Objects.isNull(csEquipmentTransferPO.getEquipmentIds())){
                String equipmentIds = csEquipmentTransferPO.getEquipmentIds();
                String[] split = equipmentIds.split(",");
                for (int i = 0; i < split.length; i++) {
                    if(Objects.equals(collect1.get(DicDataEnum.AUTHORITY_TRANSFER.getCode()),csEquipmentTransferPO.getEventType())){
//                        csDeviceUserPOService.transfer(split[i]);

                    }
                    else if(Objects.equals(collect1.get(DicDataEnum.DATA_RECOVERY.getCode()),csEquipmentTransferPO.getEventType())){
                        csDeviceUserPOService.recovery(split[i]);
                    }
                }
            }

            csEquipmentTransferPO.setStatus(2);
            this.updateById(csEquipmentTransferPO);
            log.info("更新成功");

        });

        return true;
    }


}
