package com.njcn.csdevice.controller.equipment;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.csdevice.enums.DeviceOperate;
import com.njcn.csdevice.pojo.param.UserDevParam;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.vo.DevCountVO;
import com.njcn.csdevice.pojo.vo.DevUserVO;
import com.njcn.csdevice.service.CsDeviceUserPOService;
import com.njcn.csharmonic.api.EventUserFeignClient;
import com.njcn.csharmonic.param.CsEventUserQueryParam;
import com.njcn.web.advice.DeviceLog;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
* (cs_equipment_transfer)表控制层
*
* @author xxxxx
*/
@Slf4j
@RestController
@RequestMapping("/deviceUser")
@Api(tags = " 设备用户操作")
@AllArgsConstructor
public class DeviceUserController extends BaseController {

    private final CsDeviceUserPOService csDeviceUserPOService;
    private final EventUserFeignClient eventUserFeignClient;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/add")
    @ApiOperation("新增设备扫码设备用户绑定")
    @ApiImplicitParam(name = "id", value = "设备id", required = true)
    public HttpResult<Boolean> add(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("add");

        Boolean flag = csDeviceUserPOService.add (id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/share")
    @ApiOperation("设备扫码分享")
    @ApiImplicitParam(name = "ids", value = "设备id集合", required = true)
    @DeviceLog(operateType = DeviceOperate.SHARE)
    public HttpResult<Boolean> share(@RequestParam("ids") List<String> ids){
        String methodDescribe = getMethodDescribe("share");

        Boolean flag = csDeviceUserPOService.share (ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addList")
    @ApiOperation("批量新增用户设备关系")
    @ApiImplicitParam(name = "list", value = "集合", required = true)
    public HttpResult<Boolean> add(@RequestBody List<CsDeviceUserPO> list){
        String methodDescribe = getMethodDescribe("add");
        csDeviceUserPOService.saveBatch(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
    /**
     * @Description: 统计当前工程下的设备
     * @Param:
     * @return: com.njcn.common.pojo.response.HttpResult<java.lang.Boolean>
     * @Author: clam
     * @Date: 2023/7/5
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/devCount")
    @ApiOperation("设备统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工程id"),
            @ApiImplicitParam(name = "time", value = "时间")
    })
    public HttpResult<DevCountVO> devCount(@RequestParam("id") String id,@RequestParam("time") String time){
        String methodDescribe = getMethodDescribe("devCount");
        DevCountVO result = csDeviceUserPOService.devCount(id,time);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/transfer")
    @ApiOperation("设备转移(直接扫码不用申请)")
    @ApiImplicitParam(name = "ids", value = "设备Id集合", required = true)
    @DeviceLog(operateType = DeviceOperate.TRANSFER)
    public HttpResult<Boolean> transfer(@RequestParam("ids") List<String> ids,@RequestParam("userId") String userId){
        String methodDescribe = getMethodDescribe("transfer");

        Boolean flag = csDeviceUserPOService.transfer (ids,userId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/delete")
    @ApiOperation("设备删除")
    @ApiImplicitParam(name = "eid", value = "设备Id", required = true)
    @DeviceLog(operateType = DeviceOperate.DELETE_1)
    public HttpResult<Boolean> delete(@RequestParam("eid") String eid){
        String methodDescribe = getMethodDescribe("delete");

        Boolean flag = csDeviceUserPOService.delete (eid);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/cancelShare")
    @ApiOperation("取消分享")
    @ApiImplicitParam(name = "eid", value = "设备Id", required = true)
    @DeviceLog(operateType = DeviceOperate.CANCELSHARE)
    public HttpResult<Boolean> cancelShare(@RequestParam("eid") String eid ,@RequestParam("userId") String userId){
        String methodDescribe = getMethodDescribe("cancelShare");
        Boolean flag = csDeviceUserPOService.cancelShare (eid,userId);

        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryUserById")
    @ApiOperation("设备主子用户关系")
    @ApiImplicitParam(name = "devId", value = "设备Id", required = true)
    public HttpResult<DevUserVO> queryUserById(@RequestParam("devId") String devId){
        String methodDescribe = getMethodDescribe("queryUserById");

        DevUserVO devUserVO = csDeviceUserPOService.queryUserById (devId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, devUserVO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/findUserById")
    @ApiOperation("根据设备Id查询用户集合")
    @ApiImplicitParam(name = "id", value = "设备Id", required = true)
    public HttpResult<List<String>> findUserById(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("findUserById");
        List<String> list = csDeviceUserPOService.findUserById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/findDevByUserId")
    @ApiOperation("根据用户Id查询设备集合")
    @ApiImplicitParam(name = "id", value = "用户id", required = true)
    public HttpResult<List<String>> findDevByUserId(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("findDevByUserId");
        List<String> list = csDeviceUserPOService.findDevByUserId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/channelDevByUserId")
    @ApiOperation("根据用户Id解绑设备集合")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<List<String>> channelDevByUserId(@RequestBody UserDevParam param){
        String methodDescribe = getMethodDescribe("channelDevByUserId");
        csDeviceUserPOService.channelDevByUserId(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getList")
    @ApiOperation("根据设备集合获取数据")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<List<CsDeviceUserPO>> getList(@RequestBody UserDevParam param){
        String methodDescribe = getMethodDescribe("getList");
        List<CsDeviceUserPO> list = csDeviceUserPOService.getList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getIdList")
    @ApiOperation("获取事件id集合")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<List<String>> getIdList(@RequestParam("param") String param){
        String methodDescribe = getMethodDescribe("getIdList");
        List<String> list = new ArrayList<>();
        CsEventUserQueryParam param1 = new CsEventUserQueryParam();
        param1.setUserId(RequestUtil.getUserIndex());
        param1.setStartTime(null);
        param1.setEndTime(null);
        param1.setEventIds(null);

        if (Objects.equals(param, "1")) {
            list = eventUserFeignClient.queryTempHarmonic(param1).getData();
        } else if (Objects.equals(param, "3")) {
            list = eventUserFeignClient.queryAlarmEvent(param1).getData();
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

}
