package net.northking.cctp.device.api.mobile;

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.device.api.mobile.service.CdMobileDeviceApiService;
import net.northking.cctp.device.api.pcDevice.service.CdPcDeviceApiService;
import net.northking.cctp.device.db.entity.CdDeviceTenant;
import net.northking.cctp.device.dto.device.CdMobileDeviceDetailDto;
import net.northking.cctp.device.dto.device.CdMobileDeviceQueryDto;
import net.northking.cctp.device.dto.device.CdMobileDeviceRecycleDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "设备信息(SaaS)")
@RequestMapping("/v1")
@RestController
public class CdMobileDeviceSaaSCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(CdMobileDeviceSaaSCtrl.class);

    @Autowired
    private CdMobileDeviceApiService apiService;

    @Autowired
    private CdPcDeviceApiService cdPcDeviceApiService;

    @ApiOperation(value = "分配设备(租户)")
    @RequiredPermissions({"ui-sassDistribution","ui-sassBatchDistribute","ui-PCdistribute","ui-sassPCDistributes"})
    @PostMapping(value = "/device/allocate/tenant",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> allocateFromTenant(
            @RequestBody CdMobileDeviceRecycleDto dto){
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String result = this.apiService.allocateTenant(dto);
        return wrapper.success(result);
    }


    @ApiOperation(value = "回收设备(租户)")
    @RequiredPermissions({"ui-sassRecycle","ui-sassBatchRecyle","ui-sassPCRecycles","ui-PCrecycle"})
    @PostMapping(value = "/device/recycle/tenant",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> recycleFromTenant(
            @RequestBody List<String> deviceId){
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String result = this.apiService.recycleTenant(deviceId);
        return wrapper.success(result);
    }


    @ApiOperation(value = "延长租户使用时间")
    @RequiredPermissions({"ui-sassPostpone","ui-PCdelay"})
    @PostMapping(value = "/device/extend/tenant/time-limit",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> extendTenantTimeLimit(
            @RequestBody CdDeviceTenant dto){
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = this.apiService.extendTenant(dto);
        return wrapper.success(result);
    }


    @ApiOperation(value = "分页查询列表(租户)")
    @RequiredPermissions("sassDeviceManage")
    @PostMapping(value = "/paging/query/mobile-devices/tenant",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Pagination<CdMobileDeviceDetailDto>> pagingQueryByTenant(
            @RequestBody QueryByPage<CdMobileDeviceQueryDto> queryByPage) {
        ResultWrapper<Pagination<CdMobileDeviceDetailDto>> wrapper = new ResultWrapper<>();
        Pagination<CdMobileDeviceDetailDto> page = apiService.pagingQueryByTenant(queryByPage);
        return wrapper.success(page);
    }

    @ApiOperation(value = "分页查询列表(未分配给租户的设备)")
    @PostMapping(value = "/paging/query/mobile-devices/none/tenant",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Pagination<CdMobileDeviceDetailDto>> pagingNoneTenant(
            @RequestBody QueryByPage<CdMobileDeviceQueryDto> queryByPage) {
        ResultWrapper<Pagination<CdMobileDeviceDetailDto>> wrapper = new ResultWrapper<>();
        Pagination<CdMobileDeviceDetailDto> page = apiService.pagingNoneTenant(queryByPage);
        return wrapper.success(page);
    }

    @ApiOperation(value = "分页查询列表(已分配给租户的设备)")
    @PostMapping(value = "/paging/query/mobile-devices/mobileByTenant",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Pagination<CdMobileDeviceDetailDto>> mobileByTenant(
            @RequestBody QueryByPage<CdMobileDeviceQueryDto> queryByPage) {
        ResultWrapper<Pagination<CdMobileDeviceDetailDto>> wrapper = new ResultWrapper<>();
        Pagination<CdMobileDeviceDetailDto> page = apiService.mobileByTenant(queryByPage);
        return wrapper.success(page);
    }


}
