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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.feesapi.common.Excel.ExcleFileUtil;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxRkssq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.RkstjVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxRkssqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxRkssqService;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import com.tofly.feesapi.yhgl.entity.dto.YhkhjbxxCbbWfpFilter;
import com.tofly.feesapi.yhgl.service.YhKhjbxxService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/wxrkssq")
@Api(tags = "微信-人口数变更申请接口")
public class WxRkssqController {

    @Autowired
    private WxRkssqService wxRkssqService;
    @Autowired
    private YhKhjbxxService yhKhjbxxService;


    @GetMapping("/getpage")
    @ApiOperation(value = "自定义分页查询")
    public ResultRespone<Page<WxRkssqVo>> getWxDbsqPage(Page page, @ModelAttribute WxCommonDto dto) {
        ResultRespone<Page<WxRkssqVo>> resultRespone = new ResultRespone<>();
        try {
            Page<WxRkssqVo> wxPage = wxRkssqService.getWxRkssqPage(page, dto);
            resultRespone.setCode(1);
            resultRespone.setResult(wxPage);
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getWxRkssqPage(Page page, @ModelAttribute WxRkssq wxRkssq) {
        return ResultRespone.success(wxRkssqService.page(page, Wrappers.query(wxRkssq)));
    }


    @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(wxRkssqService.getById(id));
    }


    @ApiOperation(value = "新增微信-人口数变更申请")
    @PostMapping
    public ResultRespone save(@RequestBody WxRkssq wxRkssq) {
        return ResultRespone.success(wxRkssqService.save(wxRkssq));
    }


    @ApiOperation(value = "修改微信-人口数变更申请")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxRkssq wxRkssq) {
        if (wxRkssq.getState().equals("2")) {
            wxRkssqService.updateById(wxRkssq);
            //同步用户档案状态
            if (yhKhjbxxService.lambdaUpdate()
                    .set(YhKhjbxx::getRks, Long.parseLong(wxRkssq.getRegisterZxrks()))
                    .eq(YhKhjbxx::getYhbh, wxRkssq.getRegisterYhbh())
                    .update()) {
                return ResultRespone.success("用户档案人口数更新成功");
            } else {
                return ResultRespone.failed("用户档案人口数更新失败");
            }
        } else {
            return ResultRespone.success(wxRkssqService.updateById(wxRkssq));
        }
    }


    @ApiOperation(value = "通过ID删除微信-人口数变更申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable String id) {
        return ResultRespone.success(wxRkssqService.removeById(id));
    }


    @ApiOperation(value = "通过ID批量删除微信-人口数变更申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeByIds(String ids) {
        return ResultRespone.success(wxRkssqService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @ApiOperationSupport(order = 15)
    @GetMapping("/export")
    @ApiOperation(value = "导出人口数统计")
    public void export(HttpServletResponse response, @ModelAttribute YhkhjbxxCbbWfpFilter filter) throws IOException {
        List<RkstjVo> rkstjVos = wxRkssqService.SelectRkstj(filter);
        ExcleFileUtil.exportExcel(response, rkstjVos, RkstjVo.class, "人口数统计.xlsx", filter.getExportFields());
    }

    @ApiOperationSupport(order = 15)
    @GetMapping("/getPageRkstj")
    @ApiOperation(value = "查询人口数统计")
    public ResultRespone<PageResult<RkstjVo>> getPageRkstj(PageDto pageDto, @ModelAttribute YhkhjbxxCbbWfpFilter filter) throws IOException {
        PageResult<RkstjVo> pageResult = wxRkssqService.getPageRkstj(pageDto, filter);
        return ResultRespone.success(pageResult);
    }
}
