package com.sptg.permissions.biz.controller;

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.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.permissions.api.dto.DeviceAuthorizeDto;
import com.sptg.permissions.api.dto.RegionDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.DeviceAuthorize;
import com.sptg.permissions.api.entity.Region;
import com.sptg.permissions.api.feigns.DeviceAuthorizeFeign;
import com.sptg.permissions.biz.service.DeviceAuthorizeServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
 * title: DeviceAuthorizeController
 * package: com.sptg.permissions.biz.controller
 * description: 设备授权-控制器
 *
 *
 */
@RestController
@RequiredArgsConstructor
public class DeviceAuthorizeController implements DeviceAuthorizeFeign {

    private final DeviceAuthorizeServiceInterface deviceAuthorizeServiceInterface;
    private final DeviceFeign deviceFeign;

    @Override
    @PostMapping(value = "/device/authorize/dy/pagingQuery")
    public Page<DeviceAuthorize> pagingQueryDy(@RequestBody PageBean<DeviceAuthorize,DeviceAuthorizeDto> pageBean) {
        DeviceAuthorizeDto dto = pageBean.getDto();
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",dto.getOrgPosition(), SqlLike.RIGHT);
        if (null != dto.getXqIdList() && dto.getXqIdList().size() > OperationConstant.ZERO){
            wrapper.in("l_xq_id",dto.getXqIdList());
        }
        wrapper.isNotNull("l_dy_id");
        wrapper.groupBy("l_dy_id");
        return this.deviceAuthorizeServiceInterface.selectPage(pageBean,wrapper);
    }

    @Override
    @PostMapping(value = "/device/authorize/pagingQuery")
    public Page<DeviceAuthorize> pagingQuery(@RequestBody PageBean<DeviceAuthorize,DeviceAuthorizeDto> pageBean) {
        DeviceAuthorizeDto dto = pageBean.getDto();
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",dto.getOrgPosition(), SqlLike.RIGHT);
        if (null != dto.getXqIdList() && dto.getXqIdList().size() > OperationConstant.ZERO){
            wrapper.in("l_xq_id",dto.getXqIdList());
        }
        if (null != dto.getDyId()){
            wrapper.eq("l_dy_id",dto.getDyId());
        }
        wrapper.like(dto.getDeviceSerialNumber() != null,"s_device_serial_number",dto.getDeviceSerialNumber(),SqlLike.DEFAULT);
        wrapper.groupBy("l_device_id");
        return this.deviceAuthorizeServiceInterface.selectPage(pageBean,wrapper);
    }

    @Override
    @PostMapping(value = "/device/specialAuthorize/pagingQuery")
    public Page<DeviceAuthorize> specialDevicesPagingQuery(@RequestBody PageBean<DeviceAuthorize, DeviceAuthorizeDto> pageBean) {
        DeviceAuthorizeDto dto = pageBean.getDto();
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",dto.getOrgPosition(), SqlLike.RIGHT);
        if (null != dto.getXqIdList() && dto.getXqIdList().size() > OperationConstant.ZERO){
            wrapper.in("l_xq_id",dto.getXqIdList());
        }
        if (null != dto.getDyId()){
            wrapper.eq("l_dy_id",dto.getDyId());
        }
        if (null != dto.getDeviceIdList()){
            if(dto.getDeviceIdList().size() > OperationConstant.ZERO){
                wrapper.in("l_device_id",dto.getDeviceIdList());
            }else{
                wrapper.in("l_device_id", new Long[]{-1L});
            }
        }
        wrapper.groupBy("l_device_id");
        return this.deviceAuthorizeServiceInterface.selectPage(pageBean,wrapper);
    }

    @Override
    @PostMapping(value = "/device/authorize/region/pagingQuery")
    public Page<Region> pagingQueryRegion(@RequestBody PageBean<Region,RegionDto> pageBean) {
        return this.deviceAuthorizeServiceInterface.pagingQueryRegion(pageBean.getDto(),pageBean);
    }

    @Override
    @PostMapping(value = "/device/authorize/getDeviceAccount")
    public ResultBean<Map<String,Long>> getDeviceCoverAccount(@RequestBody DeviceAuthorizeDto dto) {
        Map<String,Long> map = new HashMap<>(2);
        dto.setAddressType(OperationConstant.ONE);
        long provinceCount = this.deviceAuthorizeServiceInterface.getDeviceCoverAccount(dto);
        map.put("provinceCount",provinceCount);
        dto.setAddressType(OperationConstant.TWO);
        long cityCount = this.deviceAuthorizeServiceInterface.getDeviceCoverAccount(dto);
        map.put("cityCount",cityCount);
        dto.setIsAllType(OperationConstant.ONE);
        long totalCount = this.deviceAuthorizeServiceInterface.getDeviceAccount(dto);
        map.put("totalCount",totalCount);
        dto.setIsAllType(OperationConstant.TWO);
        long monthCount = this.deviceAuthorizeServiceInterface.getDeviceAccount(dto);
        map.put("monthCount",monthCount);
        Device device = new Device();
        device.setOrgPosition(dto.getOrgPosition());
        long offlineCount = this.deviceFeign.getOfflineDeviceAccount(device);
        map.put("offlineCount",offlineCount);

        return new ResultBean<>(ResultEnum.SUCCESS,map);
    }

    @Override
    @PostMapping(value = "/device/authorize/getDeviceCount")
    public Long getDeviceCount(@RequestBody DeviceAuthorizeDto dto) {
        return this.deviceAuthorizeServiceInterface.getDeviceCount(dto);
    }

    @Override
    @PostMapping(value = "/device/authorize/getDeviceCountForArea")
    public List<Map<String,Object>> getDeviceCountForArea(@RequestBody DeviceAuthorizeDto dto){
        return this.deviceAuthorizeServiceInterface.getDeviceCountForArea(dto);
    }

    @Override
    @PostMapping(value = "/device/authorize/getDeviceCountForInstallPathType")
    public Map<String,Object> getDeviceCountForInstallPathType(@RequestBody DeviceAuthorizeDto dto){
        return this.deviceAuthorizeServiceInterface.getDeviceCountForInstallPathType(dto);
    }

    @Override
    @PostMapping(value = "/device/authorize/getDeviceCountForMonth")
    public List<Map<String,Object>> getDeviceCountForMonth(@RequestBody DeviceAuthorizeDto dto){
        return this.deviceAuthorizeServiceInterface.getDeviceCountForMonth(dto);
    }

    @Override
    @GetMapping(value = "/device/authorize/getDeviceAuthorizeOrgList/{serialNumber}")
    public List<String> getDeviceAuthorizeOrgList(@PathVariable(value = "serialNumber") String serialNumber) {
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.eq("s_device_serial_number",serialNumber);
        List<DeviceAuthorize> list = this.deviceAuthorizeServiceInterface.selectList(wrapper);
        return list.stream().map(DeviceAuthorize::getOrgPosition).collect(Collectors.toList());
    }

    @Override
    @GetMapping(value = "/device/authorize/getDeviceIdListByOrg")
    public List<Long> getDeviceIdListByOrg(@RequestParam(value = "orgPosition") String orgPosition) {
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",orgPosition,SqlLike.RIGHT);
        wrapper.groupBy("l_device_id");
        return this.deviceAuthorizeServiceInterface.selectList(wrapper)
                .stream().map(DeviceAuthorize::getDeviceId).collect(Collectors.toList());
    }
}
