package com.securitycontrol.background.controller;

import com.securitycontrol.background.service.DeviceService;
import com.securitycontrol.background.service.UserProService;
import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.page.TableDataInfo;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import com.securitycontrol.entity.background.dto.DeviceDto;
import com.securitycontrol.entity.background.vo.DeviceVo;
import com.securitycontrol.entity.background.vo.UserProVo;
import com.securitycontrol.entity.system.SystemGlobal;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户工程 分配
 * 接口层
 * @author 黑子
 */
@RestController
@RequestMapping("/user/pro/")
@Slf4j
public class UserProController extends BaseController {

    @Autowired
    private UserProService service;


    @GetMapping("getUserList")
    @Log(title = "用户分配", menu = "基础管理->用户分配", grade = OperationType.QUERY_BUSINESS, details = "查询用户分配列表", type = "业务日志")
    public TableDataInfo getUserList(UserProVo dto) {
        try{
            dto.setRoleCode(SystemGlobal.ROLE_MANAGE);
            startPage();
            List<UserProVo> list = service.getUserList(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }

    @GetMapping("getFpProList")
    public TableDataInfo getFpProList(UserProVo dto) {
        try{
            startPage();
            List<UserProVo> list = service.getFpProList(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }

    @GetMapping("getProList")
    public TableDataInfo getProList(UserProVo dto) {
        try{
            startPage();
            List<UserProVo> list = service.getProList(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }

    /**
     * 新增用户工程
     * @param dto
     * @return
     */
    @PostMapping("addUserPro")
    @Log(title = "分配工程", menu = "设备管理->新增设备", grade = OperationType.ADD_BUSINESS, details = "分配工程", type = "业务日志")
    public Result<String> addUserPro(@RequestBody @Valid UserProVo dto) {
        return service.addUserPro(dto);
    }

    /**
     * 删除 工程
     * @param dto
     * @return
     */
    @PostMapping("delUserPro")
    @Log(title = "移除工程", menu = "设备管理->新增设备", grade = OperationType.DELETE_BUSINESS, details = "移除已分配的工程", type = "业务日志")
    public Result<String> delUserPro(@RequestBody @Valid UserProVo dto) {
        return service.delUserPro(dto);
    }

}
