package com.tofly.feesapi.wxgl.wechatofficalacctmgt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxDtbm;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxDtbmService;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxGsdt;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxGsdtMapper;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxGsdtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/wechatofficalacctmgt/wxgsdt")
@Api(tags = "微信公众号-公示公告接口")
public class WxGsdtController {
    @Autowired
    private WxGsdtService wxGsdtService;
    @Autowired
    private WxDtbmService wxDtbmService;
    @Autowired
    private WxGsdtMapper wxGsdtMapper;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页显示数据条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pages", value = "共几页,输出参数", dataType = "int"),
            @ApiImplicitParam(name = "maxLimit", value = "每页最大显示数据", dataType = "int"),
            @ApiImplicitParam(name = "orders[0].asc", value = "排序,bool类型", dataType = "String"),
            @ApiImplicitParam(name = "orders[0].column", value = "排序,字段名", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "String"),
            @ApiImplicitParam(name = "searchText", value = "模糊查询(标题)", dataType = "String")
    })
    public ResultRespone getWxGsdtPage(Page page, String searchText, String type) {
        QueryWrapper<WxGsdt> queryWrapper = new QueryWrapper<WxGsdt>();
        queryWrapper.eq("1", "1");
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.and(i -> i.like("title", searchText));
        }
        if (StringUtils.isNotBlank(type) && type != "all") {
            queryWrapper.in("dt_type", type);
        }
        Page<WxGsdt> wxpage = wxGsdtService.page(page, queryWrapper);

        List<WxGsdt> wxList = wxpage.getRecords();

        for (WxGsdt wxGsdt : wxList) {
            //动态编码
            List<WxDtbm> wxDtbmsList = wxDtbmService.list();
            Map<String, String> map = new HashMap<>();
            wxDtbmsList.forEach(e -> map.put(e.getId(), e.getName()));
            //类型
            wxGsdt.setDtTypeName(map.get(wxGsdt.getDtType()));
            wxGsdt.setStateName(wxGsdt.getState().equals("1") ? "启用" : "未启用");
        }
        return ResultRespone.success(wxpage);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    public ResultRespone getById(@PathVariable("id") Integer id) {
        return ResultRespone.success(wxGsdtService.getById(id));
    }


    @ApiOperation(value = "新增公示公告")
    @PostMapping
    public ResultRespone save(@RequestBody WxGsdt wxGsdt) {
        //查询当前ID
        int id = wxGsdtMapper.SelectChargeMaxCode();
        wxGsdt.setId(id + 1);
        wxGsdt.setState("1");
        return ResultRespone.success(wxGsdtService.save(wxGsdt));
    }

    @ApiOperation(value = "修改公示公告")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxGsdt wxGsdt) {
        return ResultRespone.success(wxGsdtService.updateById(wxGsdt));
    }


    @ApiOperation(value = "通过ID删除公示公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Integer id) {
        return ResultRespone.success(wxGsdtService.removeById(id));
    }


    @ApiOperation(value = "通过ID批量删除公示公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(wxGsdtService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
