package com.sptg.permissions.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.DevicePath;
import com.sptg.device.api.feigns.DevicePathFeign;
import com.sptg.permissions.api.dto.DeviceAuthorizeDto;
import com.sptg.permissions.api.dto.RegionDto;
import com.sptg.permissions.api.entity.DeviceAuthorize;
import com.sptg.permissions.api.entity.Region;
import com.sptg.permissions.biz.mapper.DeviceAuthorizeMapper;
import com.sptg.permissions.biz.service.DeviceAuthorizeServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * title: DeviceAuthorizeServiceImpl
 * package: com.sptg.permissions.biz.security.impl
 * description: 设备权限-服务层实现类
 *
 *                  
 *                
 */
@Service
@RequiredArgsConstructor
public class DeviceAuthorizeServiceImpl extends ServiceImpl<DeviceAuthorizeMapper,DeviceAuthorize> implements DeviceAuthorizeServiceInterface {


    /* -------------------------------------------------------------------------------------------------------------- */

    @Override
    public List<DeviceAuthorize> selectByRootOrgId(Long orgId) {
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.eq("l_device_root_org_id",orgId);
        return this.selectList(wrapper);
    }

    @Override
    public void deleteByOrgPositionOrResidentialId(String position, Long residentialId) {
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",position, SqlLike.RIGHT);
        wrapper.or().eq("l_xq_id",residentialId);
        this.delete(wrapper);
    }

    @Override
    public void deleteByOrgPositionAndResidentialId(String position, Long residentialId) {
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",position, SqlLike.RIGHT);
        wrapper.eq("l_xq_id",residentialId);
        this.delete(wrapper);
    }

    @Override
    public Page<Region> pagingQueryRegion(RegionDto dto,Page<Region> page) {
        List<Region> regionList = this.baseMapper.selectAuthorizeRegion(dto, page);
        if (null == regionList){
            regionList = new ArrayList<>(0);
        }
        page.setRecords(regionList);
        return page;
    }

    private final DevicePathFeign devicePathFeign;

    @Override
    public DeviceAuthorize createAuthorizeObj(Device device) {
        DeviceAuthorize authorize = new DeviceAuthorize();
        //  授权设备Id
        authorize.setDeviceId(device.getId());
        //  设备小区,单元信息
        authorize.setXqId(device.getXqId());
        authorize.setDyId(device.getUnitId());
        //  设备省市区信息
        authorize.setDeviceRegionProvince(device.getRegionProvince());
        authorize.setDeviceRegionCity(device.getRegionCity());
        authorize.setDeviceRegionArea(device.getRegionArea());
        //  设备信息 [ 名称,序列号,类型,创建时间,设备位置类型 ]
        authorize.setDeviceName(device.getName());
        authorize.setDeviceSerialNumber(device.getSerialNumber());
        authorize.setDeviceTypeNumber(device.getTypeNumber());
        authorize.setDeviceCreateDate(device.getCreateDate());
        if (null != device.getInstallPathId()) {
            DevicePath devicePath = this.devicePathFeign.getEntityById(device.getInstallPathId());
            authorize.setInstallPathType(devicePath.getType());
        }
        //  设备跟机构信息
        authorize.setDeviceRootOrgId(device.getRootOrgId());
        authorize.setDeviceRootOrgName(device.getRootOrgName());
        //  创建者信息
        authorize.setCreateAccountId(device.getCreateAccountId());
        return authorize;
    }

    @Override
    public Long getDeviceCoverAccount(DeviceAuthorizeDto dto) {
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        if(OperationConstant.ONE == dto.getAddressType()){
            wrapper.setSqlSelect("COUNT(DISTINCT s_device_region_province)");
        }
        if(OperationConstant.TWO == dto.getAddressType()){
            wrapper.setSqlSelect("COUNT(DISTINCT s_device_region_city)");
        }
        wrapper.like("s_org_position",dto.getOrgPosition(),SqlLike.RIGHT);
        return (Long) this.selectObj(wrapper);
    }

    @Override
    public Long getDeviceAccount(DeviceAuthorizeDto dto) {
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("COUNT(DISTINCT l_device_id)");
        wrapper.like("s_org_position", dto.getOrgPosition(),SqlLike.RIGHT);
        //当月新增
        if (OperationConstant.TWO == dto.getIsAllType()) {
            wrapper.and("DATE_FORMAT(t_create_date,'%Y%m')=DATE_FORMAT(NOW(),'%Y%m')");
        }
        return (Long)this.selectObj(wrapper);
    }

    @Override
    public List<Map<String,Object>> getDeviceCountForArea(DeviceAuthorizeDto dto) {
        EntityWrapper<DeviceAuthorize> wrapper = new EntityWrapper<DeviceAuthorize>();
        String sqlSelect = "COUNT(DISTINCT l_device_id) AS deviceCount,s_device_region_province as regionProvince";
        wrapper.setSqlSelect(sqlSelect);
        //省市区查询
        String province = dto.getDeviceRegionProvince();
        if (StringUtils.isNotBlank(province)) {
            wrapper.eq("s_device_region_province", province);
        }
        wrapper.groupBy("s_device_region_province");
        //根据机构
        if (StringUtils.isNotBlank(dto.getOrgPosition())) {
            wrapper.like("s_org_position", dto.getOrgPosition(),SqlLike.RIGHT);
        }
        return this.selectMaps(wrapper);
    }

    @Override
    public Long getDeviceCount(DeviceAuthorizeDto dto) {
        return this.baseMapper.getCount(dto);
    }

    @Override
    public Map<String,Object> getDeviceCountForInstallPathType(DeviceAuthorizeDto dto) {
        //sql展示列
        String sqlSelect="COUNT(DISTINCT(CASE  WHEN l_dy_id IS NULL THEN l_device_id ELSE NULL END)) AS residentialDeviceCount," +
                "COUNT(DISTINCT(CASE  WHEN l_dy_id IS NOT NULL THEN l_device_id ELSE NULL END)) AS unitDeviceCount";
        EntityWrapper<DeviceAuthorize> wrapper = new EntityWrapper<DeviceAuthorize>();
        wrapper.setSqlSelect(sqlSelect);
        //根据机构
        if (StringUtils.isNotBlank(dto.getOrgPosition())) {
            wrapper.like("s_org_position", dto.getOrgPosition(),SqlLike.RIGHT);
        }
        return this.selectMap(wrapper);
    }

    @Override
    public List<Map<String,Object>> getDeviceCountForMonth(DeviceAuthorizeDto dto){
        EntityWrapper<DeviceAuthorize> wrapper=new EntityWrapper<DeviceAuthorize>();
        String sqlSelect="COUNT(DISTINCT l_device_id) AS deviceCount,DATE_FORMAT(t_device_create_data,'%m') AS month";
        wrapper.setSqlSelect(sqlSelect);
        //省
        String province=dto.getDeviceRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("s_device_region_province",province);
        }
        //市
        String city=dto.getDeviceRegionCity();
        if(StringUtils.isNotBlank(city)){
            wrapper.eq("s_device_region_city",city);
        }
        //区
        String area=dto.getDeviceRegionArea();
        if(StringUtils.isNotBlank(area)){
            wrapper.eq("s_device_region_area",area);
        };
        //年份查询
        if(StringUtils.isNotBlank(dto.getSelectYear())){
                wrapper.eq("DATE_FORMAT(t_device_create_data,'%Y')",dto.getSelectYear());
        }
        //根据机构
        if (StringUtils.isNotBlank(dto.getOrgPosition())) {
            wrapper.like("s_org_position", dto.getOrgPosition(),SqlLike.RIGHT);
        }
        //分组
        wrapper.groupBy("month");
        return this.selectMaps(wrapper);
    }

}
