package com.tofly.feesapi.wxgl.wechatbasicmgt.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.common.util.GuidGenerator;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxYwxz;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxYwxzbm;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxYwxzbmService;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxYwxzService;
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/wechatbasicmgt/wxbusiness")
@Api(tags = "微信管理—业务须知接口")
public class WxYwxzController {
    @Autowired
    private WxYwxzService wxYwxzService;
    @Autowired
    private WxYwxzbmService wxYwxzbmService;

    /**
     * 分页查询
     */
    @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 getWxBusinessPage(Page page, String searchText, String type) {
        QueryWrapper<WxYwxz> queryWrapper = new QueryWrapper<WxYwxz>();
        queryWrapper.eq("isdisabled", "1");
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.and(i -> i.like("title", searchText));
        }
        if (StringUtils.isNotBlank(type) && type != "all") {
            queryWrapper.in("type", type);
        }
        Page<WxYwxz> wxPage = wxYwxzService.page(page, queryWrapper);

        List<WxYwxz> wxList = wxPage.getRecords();

        for (WxYwxz wxYwxz : wxList) {
            //动态编码
            List<WxYwxzbm> wxYwxzbmList = wxYwxzbmService.list();
            Map<String, String> map = new HashMap<>();
            wxYwxzbmList.forEach(e -> map.put(e.getId().toString(), e.getName()));
            //类型
            wxYwxz.setTypeName(map.get(wxYwxz.getType().toString()));
        }
        return ResultRespone.success(wxPage);
    }


    /**
     * 通过id查询业务须知
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    public ResultRespone getById(@PathVariable("id") String id) {
        return ResultRespone.success(wxYwxzService.getById(id));
    }

    /**
     * 新增业务须知
     */
    @ApiOperation(value = "新增业务须知")
    @PostMapping
    public ResultRespone save(@RequestBody WxYwxz wxYwxz) {
        wxYwxz.setId(GuidGenerator.getGuid());
        wxYwxz.setIsdisabled("1");
        return ResultRespone.success(wxYwxzService.save(wxYwxz));
    }

    /**
     * 修改业务须知
     */
    @ApiOperation(value = "修改业务须知")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxYwxz wxYwxz) {
        return ResultRespone.success(wxYwxzService.updateById(wxYwxz));
    }

    /**
     * 通过id删除业务须知
     */
    @ApiOperation(value = "通过ID删除业务须知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable String id) {
        return ResultRespone.success(wxYwxzService.removeById(id));
    }

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