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.CdDeviceProject;
import net.northking.cctp.device.dto.device.*;
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 = "设备信息(Platform)")
@RequestMapping("/v1")
@RestController
public class CdMobileDevicePlatformCtrl {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(CdMobileDevicePlatformCtrl.class);

    @Autowired
    private CdMobileDeviceApiService apiService;

    @Autowired
    private CdPcDeviceApiService cdPcDeviceApiService;

    @ApiOperation(value = "设备分配(系统)")
    @RequiredPermissions({"ui-allocateDeviceByPro","ui-assignDeviceByPro","ui-mobileBatchDistribute"})
    @PostMapping(value = "/device/allocate/project",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> allocateProject(
            @RequestBody CdMobileDeviceRecycleDto dto) {
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String result = this.apiService.allocateProject(dto);
        return wrapper.success(result);
    }

    @ApiOperation(value = "回收设备(系统)")
    @RequiredPermissions({"ui-recycleDeviceByPro","ui-mobileBatchRecyle","ui-reclaimDeviceByPro"})
    @PostMapping(value = "/device/recycle/project",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> recycleFromProject(
            @RequestBody List<String> dto) {
        ResultWrapper<String> wrapper = new ResultWrapper<>();
        String result = this.apiService.recycleProject(dto);
        return wrapper.success(result);
    }

    @ApiOperation(value = "延长系统使用时间")
    @RequiredPermissions({"ui-extendProTimeLimit","ui-delayedProTimeLimit"})
    @PostMapping(value = "/device/extend/project/time-limit",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Integer> extendProjectTimeLimit(
            @RequestBody CdDeviceProject dto) {
        ResultWrapper<Integer> wrapper = new ResultWrapper<>();
        Integer result = this.apiService.extendProject(dto);
        return wrapper.success(result);
    }

    /**
     * 查询用户列表
     *
     * @param queryByPage 查询条件
     * @return 数据列表
     */
    @ApiOperation(value = "分页查询列表(平台)")
    @RequiredPermissions("mobileDeviceManage")
    @PostMapping(value = "/paging/query/mobile-devices",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Pagination<CdMobileDeviceDetailDto>> pagingQuery(
            @RequestBody QueryByPage<CdMobileDeviceQueryDto> queryByPage) {
        ResultWrapper<Pagination<CdMobileDeviceDetailDto>> wrapper = new ResultWrapper<>();
        Pagination<CdMobileDeviceDetailDto> page = apiService.pagingQuery(queryByPage);

        return wrapper.success(page);
    }

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

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

    @ApiOperation(value = "分页查询列表(脚本查询可用设备)")
    @PostMapping(value = "/paging/query/script/deviceByPro",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<Pagination<ScriptResultDto>> queryDeviceByPro(
            @RequestBody QueryByPage<CdMobileDeviceScriptDto> queryByPage) {
        ResultWrapper<Pagination<ScriptResultDto>> wrapper = new ResultWrapper<>();
        Pagination<ScriptResultDto> page = apiService.scriptDeviceByPro(queryByPage);
        return wrapper.success(page);
    }

}
