package com.usky.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.domain.BaseFacilityDevice;
import com.usky.iot.domain.BaseFacilityType;
import com.usky.iot.domain.BaseGgpFacility;
import com.usky.iot.domain.DmpDeviceInfo;
import com.usky.iot.mapper.BaseFacilityTypeMapper;
import com.usky.iot.service.BaseFacilityTypeService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.iot.service.BaseGgpFacilityService;
import com.usky.iot.service.vo.BaseGgpFacilityTypeNumVO;
import com.usky.iot.service.vo.BaseGgpFacilityTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设施类型 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-06-06
 */
@Service
public class BaseFacilityTypeServiceImpl extends AbstractCrudService<BaseFacilityTypeMapper, BaseFacilityType> implements BaseFacilityTypeService {
    @Autowired
    private BaseGgpFacilityService baseGgpFacilityService;
    @Override
    public List<BaseGgpFacilityTypeVO> typeInfo() {
        List<BaseGgpFacilityTypeVO> list = new ArrayList<>();
        LambdaQueryWrapper<BaseFacilityType> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(BaseFacilityType::getTenantId, SecurityUtils.getTenantId());
        List<BaseFacilityType> list1 = this.list(queryWrapper);
        Integer typeNum = baseMapper.selectCount(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            List<Integer> typeIdList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                typeIdList.add(list1.get(i).getId());
            }
            List<BaseGgpFacilityTypeNumVO> list2 = new ArrayList<>();
            List<BaseGgpFacilityTypeNumVO> list3 = new ArrayList<>();
            List<BaseGgpFacilityTypeNumVO> list4 = new ArrayList<>();
            List<BaseGgpFacilityTypeNumVO> list5 = new ArrayList<>();
            Integer facilityNum;
            BaseGgpFacilityTypeVO baseGgpFacilityTypeVO = new BaseGgpFacilityTypeVO();
            if (CollectionUtils.isNotEmpty(typeIdList)) {
                Integer[] a2 = typeIdList.toArray(new Integer[typeIdList.size()]);
                list2 = baseMapper.typeNumList(SecurityUtils.getTenantId());
                list3 = baseMapper.typeNormalNumList(SecurityUtils.getTenantId());
                list4 = baseMapper.typeUpkeepNumList(SecurityUtils.getTenantId());
                list5 = baseMapper.typeCloseNumList(SecurityUtils.getTenantId());
                LambdaQueryWrapper<BaseGgpFacility> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.eq(BaseGgpFacility::getTenantId, SecurityUtils.getTenantId());
                facilityNum = baseGgpFacilityService.getBaseMapper().selectCount(queryWrapper1);
                baseGgpFacilityTypeVO.setFacilityNum(facilityNum);
                baseGgpFacilityTypeVO.setTypeNum(list2.size());
            }
            List<BaseGgpFacilityTypeNumVO> list8 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(list2)) {
                for (int i = 0; i < list2.size(); i++) {
                    for (int j = 0; j < list3.size(); j++) {
                        if (list2.get(i).getId().equals(list3.get(j).getId())){
                            list2.get(i).setNormalFacilityNum(list3.get(j).getNormalFacilityNum());
                        }
                    }
                    for (int k = 0; k < list4.size(); k++) {
                        if (list2.get(i).getId().equals(list4.get(k).getId())){
                            list2.get(i).setUpkeepFacilityNum(list4.get(k).getUpkeepFacilityNum());
                        }
                    }
                    for (int g = 0; g < list5.size(); g++) {
                        if (list2.get(i).getId().equals(list5.get(g).getId())){
                            list2.get(i).setCloseFacilityNum(list5.get(g).getCloseFacilityNum());
                        }
                    }
                    list8.add(list2.get(i));
                }
                baseGgpFacilityTypeVO.setBaseGgpFacilityTypeNumVO(list8);
                list.add(baseGgpFacilityTypeVO);
            }
        }
        return list;
    }
    @Override
    public CommonPage<BaseFacilityType> typePage(String typeCode, String typeName, Integer current, Integer size) {
        IPage<BaseFacilityType> page = new Page<>(current, size);
        LambdaQueryWrapper<BaseFacilityType> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.like(StringUtils.isNotBlank(typeName),BaseFacilityType::getTypeName,
                typeName)
                .eq(StringUtils.isNotBlank(typeCode),BaseFacilityType::getTypeCode,
                        typeCode)
//                .eq(BaseFacilityType::getTenantId, SecurityUtils.getTenantId())
                .orderByDesc(BaseFacilityType::getId);
        page = this.page(page,lambdaQuery);
        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getCurrent(),page.getSize());
    }
    @Override
    public List<BaseFacilityType> typeSelect(Integer id) {
        LambdaQueryWrapper<BaseFacilityType> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(null != id,BaseFacilityType::getId,id);
//        .eq(BaseFacilityType::getTenantId, SecurityUtils.getTenantId());
        List<BaseFacilityType> list1 = this.list(lambdaQuery);
        return list1;
    }

    @Override
    public boolean add(BaseFacilityType baseFacilityType) {
        baseFacilityType.setCreatedBy(SecurityUtils.getUsername());
        baseFacilityType.setCreatedTime(LocalDateTime.now());
        baseFacilityType.setTenantId(SecurityUtils.getTenantId());
        LambdaQueryWrapper<BaseFacilityType> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.orderByDesc(BaseFacilityType::getId)
                .last(" limit 1");
        List<BaseFacilityType> list1 = this.list(lambdaQuery);
        if (CollectionUtils.isNotEmpty(list1)) {
            Integer num1 = Integer.parseInt(list1.get(0).getTypeCode())+1;
            baseFacilityType.setTypeCode(Integer.toString(num1));
        }else{
            baseFacilityType.setTypeCode("10001");
        }
        return this.save(baseFacilityType);
    }

    @Override
    public void update(BaseFacilityType baseFacilityType) {
        baseFacilityType.setUpdatedTime(LocalDateTime.now());
        baseFacilityType.setUpdatedBy(SecurityUtils.getUsername());
        this.updateById(baseFacilityType);
    }

    @Override
    public boolean remove(Integer id) {
        BaseFacilityType one = this.getById(id);
        if(one == null){
            throw new BusinessException("设施类型不存在");
        }

        LambdaQueryWrapper<BaseGgpFacility> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(BaseGgpFacility::getFacilityType, one.getTypeCode());
        int facilityNum = baseGgpFacilityService.getBaseMapper().selectCount(queryWrapper1);
        if (facilityNum>0) {
            throw new BusinessException("当前类型已有关联设施，不可删除");
        }

        return this.removeById(id);
    }
}
