package com.zhgd.xmgl.modules.basicdata.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.dto.PolicyDto;
import com.zhgd.xmgl.modules.basicdata.entity.Policy;
import com.zhgd.xmgl.modules.basicdata.service.IPolicyService;
import com.zhgd.xmgl.modules.basicdata.vo.PolicyVo;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.Map;


/**
 * @Title: Controller
 * @Description: 政策法规
 * @author： pengj
 * @date： 2023-03-02
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/policy")
@Slf4j
@Api(tags = "政策法规管理")
public class PolicyController {
    @Autowired
    private IPolicyService policyService;

    /**
     * 分页列表查询
     * @return
     */
    @OperLog(operModul = "政策法规管理", operType = "分页查询", operDesc = "分页列表查询《我发布》的通知公告")
    @ApiOperation(value = "分页列表查询《我发布》的通知公告", notes = "分页列表查询《我发布》的通知公告", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "内容", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/fromMePage")
    public Result<IPage<Policy>> fromMePageList(@RequestBody Map<String, Object> map) {
        LambdaQueryWrapper<Policy> wrapper = Wrappers.<Policy>lambdaQuery();
        Page<Policy> page = PageUtil.getPage(map);
        String title = MapUtils.getString(map, "title");
        String content = MapUtils.getString(map, "content");
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(Policy::getTitle, title);
        }
        if (StringUtils.isNotBlank(content)) {
            wrapper.like(Policy::getContent, content);
        }
        wrapper.eq(Policy::getCreateBy, SecurityUtil.getUser().getSn());
        IPage<Policy> pageList = policyService.page(page, wrapper);
        return Result.success(pageList);
    }

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "通知公告管理", operType = "分页查询", operDesc = "分页列表查询《我接收》的通知公告")
    @ApiOperation(value = " 分页列表查询《我接收》的通知公告", notes = "分页列表查询《我接收》的通知公告", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "内容", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/toMePage")
    public Result<IPage<Policy>> toMePageList(@RequestBody Map<String, Object> map) {
        IPage<Policy> pageList = policyService.toMePageList(map);
        return Result.success(pageList);
    }

    /**
     * 添加
     * @param policyVo
     * @return
     */
    @OperLog(operModul = "政策法规管理", operType = "新增", operDesc = "添加政策法规信息")
    @ApiOperation(value = " 添加政策法规信息", notes = "添加政策法规信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody PolicyVo policyVo) {
        return Result.success(policyService.saveInfo(policyVo));
    }

    /**
     * 编辑
     * @param policyVo
     * @return
     */
    @OperLog(operModul = "政策法规管理", operType = "修改", operDesc = "编辑政策法规信息")
    @ApiOperation(value = "编辑政策法规信息", notes = "编辑政策法规信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<Policy> edit(@RequestBody PolicyVo policyVo) {
        Result<Policy> result = new Result<Policy>();
        Policy policyEntity = policyService.getById(policyVo.getPolicyId());
        if (policyEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = policyService.updateInfo(policyVo);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 通过id删除
     * @param policyEntity
     * @return
     */
    @OperLog(operModul = "政策法规管理", operType = "删除", operDesc = "删除政策法规信息")
    @ApiOperation(value = "删除政策法规信息", notes = "删除政策法规信息", httpMethod = "POST")
    @ApiImplicitParam(name = "policyId", value = "政策法规ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/delete")
    public Result<Policy> delete(@RequestBody Policy policyEntity) {
        Result<Policy> result = new Result<Policy>();
        Policy policy = policyService.getById(policyEntity.getPolicyId());
        if (policy == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = policyService.delInfo(policyEntity);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * 通过id查询
     * @param policyEntity
     * @return
     */
    @OperLog(operModul = "政策法规管理", operType = "查询", operDesc = "通过id查询政策法规信息")
    @ApiOperation(value = "通过id查询政策法规信息", notes = "通过id查询政策法规信息", httpMethod = "POST")
    @ApiImplicitParam(name = "policyId", value = "政策法规ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<PolicyDto> queryById(@RequestBody Policy policyEntity) {
        Result<PolicyDto> result = new Result<PolicyDto>();
        Policy policy = policyService.getById(policyEntity.getPolicyId());
        if (policy == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(policyService.getDetail(policy));
            result.setSuccess(true);
        }
        return result;
    }
}
