package com.bonus.bracelet.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.bracelet.mapper.SidebandMapper;
import com.bonus.bracelet.mapper.TeamMgeMapper;
import com.bonus.bracelet.service.ISidebandService;
import com.bonus.bracelet.service.ITeamMgeService;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.entity.bracelet.vo.*;
import com.bonus.common.entity.file.ResourceFileVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.*;

/**
 * @className:SidebandServiceImpl
 * @author:cwchen
 * @date:2024-07-16-10:37
 * @version:1.0
 * @description:边带管理-serviceImpl
 */
@Service(value = "ISidebandService")
@Slf4j
public class SidebandServiceImpl implements ISidebandService {

    @Resource(name = "SidebandMapper")
    private SidebandMapper sidebandMapper;

    @Override
    public List<SidebandVo> getSidebandLists(SidebandVo data) {
        List<SidebandVo> list = new ArrayList<>();
        try {
            list = sidebandMapper.getSidebandLists(data);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    /**
     * 杆塔绑定设备列表
     * @param data
     * @return
     */
    @Override
    public List<SidebandDeviceVo> getDeviceLists(SidebandDeviceVo data) {
        List<SidebandDeviceVo> list = new ArrayList<>();
        try {
            list = sidebandMapper.getDeviceLists(data);
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getLyType()==0 && !list.get(i).getDeviceTypeName().equals("手提箱")){
                 String name = sidebandMapper.getDeviceLyName(list.get(i).getLyId());
                    list.get(i).setLyName(name);
                }else if(list.get(i).getLyType()==1 && !list.get(i).getDeviceTypeName().equals("手提箱")){
                 String name = sidebandMapper.getDeviceLyNameTemp(list.get(i).getLyId());
                    list.get(i).setLyName(name);
                }
                if(list.get(i).getLyName()!=null && list.get(i).getDeviceTypeName().equals("手提箱")){
                    list.get(i).setDeviceStatus(0);
                }else if(list.get(i).getLyName()==null && list.get(i).getDeviceTypeName().equals("手提箱")){
                    list.get(i).setDeviceStatus(1);
                }
            }

        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    /**
     * 杆塔未绑定设备列表
     * @param data
     * @return
     */
    @Override
    public List<SidebandDeviceVo> getDeviceNoLists(SidebandDeviceVo data) {
        List<SidebandDeviceVo> list = new ArrayList<>();
        try {
            list = sidebandMapper.getDeviceNoLists(data);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    @Override
    public AjaxResult getGtList(ProjectPowerVo dto) {
        try {
            List<ProjectPowerVo> list = sidebandMapper.getGtList(dto);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error(e.toString(),e);
            return AjaxResult.error();
        }
    }

    /**
     * 根据边带id获得详细信息
     * @param sidebandId
     * @return
     */
    @Override
    public AjaxResult selectSidebandById(Integer sidebandId) {
        try {
            SidebandVo vo = sidebandMapper.selectSidebandById(sidebandId);
            return AjaxResult.success(vo);
        } catch (Exception e) {
            log.error("边带详情", e);
            return AjaxResult.error();
        }
    }

    /**
     * 新增边带信息
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSideband(SidebandVo vo) {
        try {
            SidebandVo result = sidebandMapper.isNameExist(vo);
            if (result != null) {
                return AjaxResult.error("边带名称已存在");
            }
            int num =  sidebandMapper.addSideband(vo);
            if(num==1){
                return AjaxResult.success();
            }
            return AjaxResult.error("边带信息新增失败");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editSideband(SidebandVo vo) {
        try {
            SidebandVo result = sidebandMapper.isExist(vo);
            if (result != null) {
                return AjaxResult.error("边带名称已存在");
            }

           int num =  sidebandMapper.updateSideband(vo);
            if(num==1){
                return AjaxResult.success();
            }
            return AjaxResult.error("边带信息修改失败");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    @Override
    public String queryProjectName(String name) {
        String vo = "";
        try {
             vo = sidebandMapper.queryProjectName(name);
            return vo;
        } catch (Exception e) {
            log.error("边带详情", e);
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delSideband(SidebandVo vo) {
        try {
            int result = sidebandMapper.delSideband(vo);
            if(result==1){
                return AjaxResult.success();
            }
            return AjaxResult.error("边带信息删除失败");
        } catch (Exception e) {
            log.error(e.toString(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    /**
     * 解除绑定
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delDevice(SidebandDeviceVo vo) {
        int result = 0;
        try {
            if(vo.getDeviceTypeName().equals("手环箱")){
                result = sidebandMapper.delSh(vo);
            }else{
                 result = sidebandMapper.delDevice(vo);
            }

            if(result==1){
                return AjaxResult.success();
            }
            return AjaxResult.error("解除绑定失败");
        } catch (Exception e) {
            log.error(e.toString(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult bindDevice(int sidebandId,List<Map<String,Object>> list) {
//        if(deviceIds.length==0){
//            return AjaxResult.error("请至少选择一个要绑定的设备");
//        }
        Map<String,Object> map = new HashMap<>();
        try {
            for (int i = 0; i < list.size(); i++) {
                map = list.get(i);
                if(map.get("type").equals("手环箱")){
                    int deviceId = (int) map.get("id");
                    sidebandMapper.bindSh(sidebandId,deviceId);
                }else{
                    int deviceId = (int) map.get("id");
                    sidebandMapper.bindDevice(sidebandId,deviceId);
                }
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("绑定失败");
        }
        return AjaxResult.success("绑定成功");
    }
}
