package com.tofly.feesapi.wxgl.wechatbasicmgt.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.common.util.GuidGenerator;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxMessage;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxTstz;
import com.tofly.feesapi.wxgl.wechatbasicmgt.mapper.WxTstzMapper;
import com.tofly.feesapi.wxgl.wechatbasicmgt.model.WxTstzReq;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxMessageService;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxTstzService;
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.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/api/wechatbasicmgt/wxtsnotice")
@Api(tags = "微信推送停水通知接口")
public class WxTstzController {

    @Autowired
    private WxTstzService wxTstzService;
    @Autowired
    private WxTstzMapper wxTsTzMapper;
    @Autowired
    private WxMessageService wxMessageService;


    /**
     * 分页查询  微信推送停水通知接口
     */
    @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 = "state", value = "是否发送，0,1", dataType = "String"),
            @ApiImplicitParam(name = "editDateStart", value = "开始日期", dataType = "String"),
            @ApiImplicitParam(name = "editDateEnd", value = "结束日期", dataType = "String"),
            @ApiImplicitParam(name = "searchText", value = "模糊查询(编辑人员、标题、停水区域)", dataType = "String")
    })
    public ResultRespone getWxTsNoticePage(Page page, String searchText, String state, String editDateStart, String editDateEnd) {
        IPage<WxTstz> WxTsPage = wxTsTzMapper.selectWxTsNoticePage(page, searchText, state, editDateStart, editDateEnd);
        return ResultRespone.success(WxTsPage);
    }


    @ApiOperation(value = "新增微信推送停水通知")
    @PostMapping
    public ResultRespone save(@RequestBody WxTstz wxTsTz) {
        //查询当前ID
        BigDecimal id = wxTsTzMapper.SelectChargeMaxCode();
        wxTsTz.setId(id.add(BigDecimal.ONE));
        wxTsTz.setState("0");//推送状态
        wxTsTz.setClLock("0");
        wxTsTz.setAuditFlag("0");//未审核

        return ResultRespone.success(wxTstzService.save(wxTsTz));
    }

    /**
     * 发送微信推送停水通知
     */
    @ApiOperation(value = "发送微信推送停水通知")
    @PostMapping("/send")
    public ResultRespone send(@RequestBody WxTstzReq req) {
        return ResultRespone.success(wxTstzService.lambdaUpdate().set(WxTstz::getState, "1").eq(WxTstz::getId, req.getId()).update());
    }

    /**
     * 发送微信推送停水通知
     */
    @ApiOperation(value = "发送微信推送停水通知")
    @PostMapping("/send_06")
    public ResultRespone send_06(@RequestBody WxTstz wxTsTz) {
        //发送微信接口信息
        List<String> listpenid = wxTstzService.wx_send(wxTsTz);

        List<WxMessage> ListMessage = new ArrayList<>();
        for (int i = 0; i < listpenid.size(); i++) {
            WxMessage wxMessage = new WxMessage();
            wxMessage.setId(GuidGenerator.getGuid());
            wxMessage.setMsgType("06");
            wxMessage.setOpenid(listpenid.get(i));
            wxMessage.setFirst(wxTsTz.getTitle());
//            wxMessage.setKeyword1(wxTsTz.getStopType());//停水类型
//            wxMessage.setKeyword2(wxTsTz.getStopTime());//停水时间
            wxMessage.setKeyword3(wxTsTz.getStopArea());//停水区域
            wxMessage.setRemark(wxTsTz.getRemark());
            wxMessage.setAddDate(wxTsTz.getEditDate());
            wxMessage.setAddOcode(wxTsTz.getEditCode());
            wxMessage.setSendState("0");
            wxMessage.setIsLock("0");
            ListMessage.add(wxMessage);
        }
        return ResultRespone.success(wxMessageService.saveBatch(ListMessage));

    }

    /**
     * 发送微信推送紧急停水通知
     */
    @ApiOperation(value = "发送微信推送紧急停水通知")
    @PostMapping("/send_10")
    public ResultRespone send_10(@RequestBody WxTstz wxTsTz) {
        //发送微信接口信息
        List<String> listpenid = wxTstzService.wx_send(wxTsTz);

        List<WxMessage> ListMessage = new ArrayList<>();
        for (int i = 0; i < listpenid.size(); i++) {
            WxMessage wxMessage = new WxMessage();
            wxMessage.setId(GuidGenerator.getGuid());
            wxMessage.setMsgType("10");
            wxMessage.setOpenid(listpenid.get(i));
            wxMessage.setFirst(wxTsTz.getTitle());
//            wxMessage.setKeyword1(wxTsTz.getStopType());//停水类型
//            wxMessage.setKeyword2(wxTsTz.getStopTime());//停水时间
//            wxMessage.setKeyword3(wxTsTz.getStopArea());//停水区域
            wxMessage.setRemark(wxTsTz.getRemark());
            wxMessage.setAddDate(wxTsTz.getEditDate());
            wxMessage.setAddOcode(wxTsTz.getEditCode());
            wxMessage.setSendState("0");
            wxMessage.setIsLock("0");
            ListMessage.add(wxMessage);
        }
        return ResultRespone.success(wxMessageService.saveBatch(ListMessage));

    }

    /**
     * 发送微信推送延迟供水通知
     *
     * @param wxTsTz 微信推送停水通知
     * @return ResultRespone
     */
    @ApiOperation(value = "发送微信推送延迟供水通知")
    @PostMapping("/send_11")
    public ResultRespone send_11(@RequestBody WxTstz wxTsTz) {
        //发送微信接口信息
        List<String> listpenid = wxTstzService.wx_send(wxTsTz);

        List<WxMessage> ListMessage = new ArrayList<>();
        for (int i = 0; i < listpenid.size(); i++) {
            WxMessage wxMessage = new WxMessage();
            wxMessage.setId(GuidGenerator.getGuid());
            wxMessage.setMsgType("11");
            wxMessage.setOpenid(listpenid.get(i));
            wxMessage.setFirst(wxTsTz.getTitle());
//            wxMessage.setKeyword1(wxTsTz.getStopTime());
            wxMessage.setKeyword2(wxTsTz.getStopArea());
            wxMessage.setRemark(wxTsTz.getRemark());
            wxMessage.setAddDate(wxTsTz.getEditDate());
            wxMessage.setAddOcode(wxTsTz.getEditCode());
            wxMessage.setSendState("0");
            wxMessage.setIsLock("0");
            ListMessage.add(wxMessage);
        }
        return ResultRespone.success(wxMessageService.saveBatch(ListMessage));

    }

    @ApiOperation(value = "发送微信推送恢复供水提醒")
    @PostMapping("/send_12")
    public ResultRespone send_12(@RequestBody WxTstz wxTsTz) {
        //发送微信接口信息
        List<String> listpenid = wxTstzService.wx_send(wxTsTz);

        List<WxMessage> ListMessage = new ArrayList<>();
        for (int i = 0; i < listpenid.size(); i++) {
            WxMessage wxMessage = new WxMessage();
            wxMessage.setId(GuidGenerator.getGuid());
            wxMessage.setMsgType("12");
            wxMessage.setOpenid(listpenid.get(i));
            wxMessage.setFirst(wxTsTz.getTitle());
//            wxMessage.setKeyword1(wxTsTz.getStopTime());
//            wxMessage.setKeyword2(wxTsTz.getStopArea());
            wxMessage.setRemark(wxTsTz.getRemark());
            wxMessage.setAddDate(wxTsTz.getEditDate());
            wxMessage.setAddOcode(wxTsTz.getEditCode());
            wxMessage.setSendState("0");
            wxMessage.setIsLock("0");
            ListMessage.add(wxMessage);
        }
        return ResultRespone.success(wxMessageService.saveBatch(ListMessage));

    }


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


    @ApiOperation(value = "修改微信推送停水通知")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxTstz wxTstz) {
        return ResultRespone.success(wxTstzService.updateById(wxTstz));
    }


    @ApiOperation(value = "通过ID删除微信推送停水通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable BigDecimal id) {
        return ResultRespone.success(wxTstzService.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(wxTstzService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
