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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxWd;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxWdbm;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxWdMapper;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxWdbmService;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxWdService;
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/wxwd")
@Api(tags = "微信公众号-网点接口")
public class WxWdController {
    @Autowired
    private WxWdService wxWdService;
    @Autowired
    private WxWdbmService wxWdbmService;
    @Autowired
    private WxWdMapper wxWdMapper;


    @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 = "wdName", value = "网点名称", dataType = "String"),
            @ApiImplicitParam(name = "searchText", value = "模糊查询(地址)", dataType = "String")
    })
    public ResultRespone getWxWdPage(Page page, String searchText, String wdName) {
        IPage<WxWd> wxpage = wxWdMapper.SelectWxWdPage(page, searchText, wdName);
        List<WxWd> wxList = wxpage.getRecords();
        //动态编码
        List<WxWdbm> wxWdCodesList = wxWdbmService.list();
        Map<Integer, String> map = new HashMap<>();
        wxWdCodesList.forEach(e -> map.put(e.getId(), e.getName()));
        for (WxWd wxWd : wxList) {
            //类型
            wxWd.setWdNames(map.get(wxWd.getWdName()));
        }
        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(wxWdService.getById(id));
    }

    @ApiOperation(value = "新增网点")
    @PostMapping
    public ResultRespone save(@RequestBody WxWd wxWd) {
        //查询当前ID
        int id = wxWdMapper.SelectChargeMaxCode();
        wxWd.setId(id + 1);

        return ResultRespone.success(wxWdService.save(wxWd));
    }


    @ApiOperation(value = "修改网点")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxWd wxWd) {
        return ResultRespone.success(wxWdService.updateById(wxWd));
    }

    @ApiOperation(value = "通过ID删除网点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Integer id) {
        return ResultRespone.success(wxWdService.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(wxWdService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
