package com.njcn.cssystem.controller.baseinfo;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.cssystem.pojo.param.AppBaseInformationAddParm;
import com.njcn.cssystem.pojo.param.AppPersonSetAddParm;
import com.njcn.cssystem.pojo.po.AppBaseInformationPO;
import com.njcn.cssystem.pojo.po.AppPersonSetPO;
import com.njcn.cssystem.pojo.vo.AppBaseInformationVO;
import com.njcn.cssystem.service.AppBaseInformationService;
import com.njcn.cssystem.service.AppPersonSetService;
import com.njcn.web.controller.BaseController;
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.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/4/4  9:02【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/appinfo")
@Api(tags = "app信息")
@AllArgsConstructor
public class AppInfoController  extends BaseController {

    private final AppBaseInformationService appBaseInformationService;
    private final AppPersonSetService appPersonSetService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addAppInfo")
    @ApiOperation("新增/更新app基础信息")
    @ApiImplicitParam(name = "AppBaseInformationAddParm", value = "新增app基础信息参数", required = true)
    public HttpResult<Boolean> addAppInfo(@RequestBody @Validated AppBaseInformationAddParm AppBaseInformationAddParm){
        String methodDescribe = getMethodDescribe("addAppInfo");
        AppBaseInformationPO appBaseInformationPO = new AppBaseInformationPO ();
        BeanUtils.copyProperties (AppBaseInformationAddParm, appBaseInformationPO);
        QueryWrapper<AppBaseInformationPO> query = new QueryWrapper();
        query.eq("type",AppBaseInformationAddParm.getType());
        boolean save =false;
        List<AppBaseInformationPO> list = appBaseInformationService.list(query);
        if (CollectionUtil.isNotEmpty(list)) {
            appBaseInformationPO = list.get(0);
            appBaseInformationPO.setContent(AppBaseInformationAddParm.getContent());
            save = appBaseInformationService.updateById(appBaseInformationPO);
        }else{
             save = appBaseInformationService.save (appBaseInformationPO);

        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, save, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addPersonSet")
    @ApiOperation("新增app个人中心信息")
    @ApiImplicitParam(name = "appPersonSetAddParm", value = "新增apppp个人中心信参数", required = true)
    public HttpResult<Boolean> addPersonSet(@RequestBody @Validated AppPersonSetAddParm appPersonSetAddParm){
        String methodDescribe = getMethodDescribe("addPersonSet");
        AppPersonSetPO appPersonSetPO = new AppPersonSetPO ();
        BeanUtils.copyProperties (appPersonSetAddParm, appPersonSetPO);
        appPersonSetPO.setStatus ("1");
        boolean save = appPersonSetService.save (appPersonSetPO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, save, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPersonSet")
    @ApiOperation("查询app个人中心信息")
    public HttpResult<AppPersonSetPO> queryPersonSet(){
        String methodDescribe = getMethodDescribe("queryPersonSet");

        List<AppPersonSetPO> list = appPersonSetService.list ( );
        AppPersonSetPO appPersonSetPO = list.stream ( ).filter (temp -> Objects.equals ("1", temp.getStatus ( ))).collect (Collectors.toList ( )).get (0);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, appPersonSetPO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryAppInfo")
    @ApiOperation("查询app个人中心信息详情")
    public HttpResult<AppBaseInformationVO> queryPersonSet(@RequestParam("id")String id ){
        String methodDescribe = getMethodDescribe("queryPersonSet");
        AppBaseInformationVO appBaseInformationVO = new AppBaseInformationVO();
        AppBaseInformationPO byId = appBaseInformationService.getById (id);
        BeanUtils.copyProperties (byId,appBaseInformationVO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, appBaseInformationVO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryAppInfoByType")
    @ApiOperation("查询app个人中心信息详情")
    public HttpResult<AppBaseInformationVO> queryAppInfoByType(@RequestParam("type")String type ){
        String methodDescribe = getMethodDescribe("queryAppInfoByType");
        AppBaseInformationVO appBaseInformationVO = new AppBaseInformationVO();

        AppBaseInformationPO byId = appBaseInformationService.getByType(type);
        BeanUtils.copyProperties (byId,appBaseInformationVO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, appBaseInformationVO, methodDescribe);
    }
}
