package com.njcn.user.controller;


import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
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.common.utils.LogUtil;
import com.njcn.user.pojo.param.HomePageParam;
import com.njcn.user.pojo.po.HomePage;
import com.njcn.user.service.IHomePageService;
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.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/homePage")
@Api(tags = "驾驶舱配置管理")
@AllArgsConstructor
public class HomePageController extends BaseController {

    private final IHomePageService homePageService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/add")
    @ApiOperation("新增驾驶舱")
    @ApiImplicitParam(name = "homePage", value = "驾驶舱实体", required = true)
    public HttpResult<Boolean> add(@RequestBody HomePageParam homePage){
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log,  "{},驾驶舱实体为：{}", methodDescribe,homePage);
        boolean result = homePageService.add(homePage);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true,methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false,methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.DELETE)
    @DeleteMapping("/delete")
    @ApiOperation("删除驾驶舱")
    @ApiImplicitParam(name = "id", value = "驾驶舱id", required = true)
    public HttpResult<Boolean> delete(@RequestParam String id){
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log,  "{},驾驶舱实体为：{}", methodDescribe,id);
        boolean result = homePageService.delete(id);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true,methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false,methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改驾驶舱")
    @ApiImplicitParam(name = "homePageUpdate", value = "驾驶舱实体", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated HomePageParam.HomePageUpdateParam homePageUpdate){
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log,  "{},修改的驾驶舱实体：{}", methodDescribe,homePageUpdate);
        boolean result = homePageService.update(homePageUpdate);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getHomePages")
    @ApiOperation("用户驾驶舱")
    public HttpResult<List<HomePage>> getHomePages(){
        String methodDescribe = getMethodDescribe("getHomePages");
        List<HomePage> list = homePageService.getHomePagesByUserId(RequestUtil.getUserIndex());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getHomePageById")
    @ApiOperation("驾驶舱详情")
    @ApiImplicitParam(name = "id", value = "驾驶舱id", required = true)
    public HttpResult<HomePage> getHomePageById(String id){
        String methodDescribe = getMethodDescribe("getHomePageById");
        LogUtil.njcnDebug(log,  "{},传入的ID为：{}", methodDescribe,id);
        HomePage homePage = homePageService.getHomePageById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,homePage,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getUsedHomePage")
    @ApiOperation("已使用驾驶舱")
    @ApiImplicitParam(name = "path", value = "驾驶舱路径", required = true)
    public HttpResult<List<String>> getUsedHomePage(String path){
        String methodDescribe = getMethodDescribe("getUsedHomePage");
        LogUtil.njcnDebug(log,  "{},传入的驾驶舱路径为：{}", methodDescribe,path);
        List<String> list = homePageService.getUsedHomePage(path);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,list,methodDescribe);
    }

}

