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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.bjgl.entity.YhCbkxx;
import com.tofly.feesapi.bjgl.service.YhCbkxxService;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxUser;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxUserLs;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxUserLsService;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxUserService;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import com.tofly.feesapi.yhgl.service.YhKhjbxxService;
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.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/wechatwebsite/BindCustomer")
@Api(tags = "微网厅-用户绑定接口bak无用")
public class BindCustomerWController {
    @Autowired
    private YhKhjbxxService daCustomerService;
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private WxUserLsService wxUserLsService;
    @Autowired
    private YhCbkxxService daMeterService;


    @GetMapping("/selectCustomer")
    @ApiOperation(value = "查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "customerName", value = "客户姓名", required = true, dataType = "String")
    })
    public ResultRespone selectCustomer(HttpServletResponse response, @RequestParam("openid") String openid, @RequestParam("customerNo") String customerNo, @RequestParam("customerName") String customerName) throws IOException {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("客户编号不能为空！");
            return resultModel;
        }
        if (StringUtils.isBlank(customerName)) {
            resultModel.setCode(-1);
            resultModel.setMessage("客户名称不能为空！");
            return resultModel;
        }
        //endregion

        QueryWrapper<YhKhjbxx> queryWrapper = new QueryWrapper<YhKhjbxx>();

        queryWrapper.eq("customer_no", customerNo).notIn("customer_state", '3', '4');
        List<YhKhjbxx> daCustomerList = daCustomerService.list(queryWrapper);
        if (daCustomerList.size() == 0) {
            resultModel.setCode(1);
            resultModel.setMessage("客户名称不存在！");
            return resultModel;
        }
        queryWrapper.clear();
        queryWrapper.eq("customer_no", customerNo).notIn("customer_state", '3', '4').like("customer_name", customerName);
        daCustomerList = daCustomerService.list(queryWrapper);
        if (daCustomerList.size() == 0 || daCustomerList.size() > 1) {
            resultModel.setCode(1);
            resultModel.setMessage("客户名称不匹配，请重新输入！");
            return resultModel;
        }
        QueryWrapper<WxUser> queryWrapperWx = new QueryWrapper<WxUser>();
        queryWrapperWx.eq("customer_no", customerNo).eq("openid", openid);
        List<WxUser> wxUserList = wxUserService.list(queryWrapperWx);
        if (wxUserList.size() > 0) {

            if (!StringUtils.isBlank(wxUserList.get(0).getBindFlag()) && wxUserList.get(0).getBindFlag() == "1") {
                resultModel.setCode(3);
                resultModel.setMessage("该收费号已绑定微信！");
                return resultModel;
            }
        }
        queryWrapperWx.clear();
        queryWrapperWx.eq("openid", openid).eq("bind_flag", "1");
        wxUserList = wxUserService.list(queryWrapperWx);

        if (wxUserList.size() > 10) {
            resultModel.setCode(5);
            resultModel.setMessage("此微信已绑定满10个收费号！");
            return resultModel;
        }
        queryWrapperWx.clear();
        queryWrapperWx.eq("customer_no", customerNo).eq("bind_flag", "1");
        wxUserList = wxUserService.list(queryWrapperWx);

        if (wxUserList.size() > 10) {
            resultModel.setCode(5);
            resultModel.setMessage("此收费号已绑定满10个微信号!");
            return resultModel;
        }

        QueryWrapper<YhCbkxx> queryWrapperDaMeter = new QueryWrapper<YhCbkxx>();
        queryWrapperDaMeter.eq("customer_no", customerNo).in("meter_type", '1');
        List<YhCbkxx> daMeterList = daMeterService.list(queryWrapperDaMeter);

        if (daMeterList.size() > 0) {
            resultModel.setCode(6);
            resultModel.setMessage("IC卡用户不能绑定,请持卡到营业点缴费!！");
            return resultModel;
        }

        //region 业务处理
        queryWrapper.clear();
        queryWrapper.eq("customer_no", customerNo);
        daCustomerList = daCustomerService.list(queryWrapper);
        resultModel.setResult(daCustomerList.get(0));
        resultModel.setMessage("成功！");
        //endregion
        return resultModel;
    }

    @PostMapping("/addCustomer")
    @ApiOperation(value = "添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ResultRespone addCustomer(@RequestParam("openid") String openid, @RequestParam("customerNo") String customerNo) throws IOException {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);
        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("客户编号不能为空！");
            return resultModel;
        }
        if (StringUtils.isBlank(openid)) {
            resultModel.setCode(-1);
            resultModel.setMessage("微信openid不能为空！");
            return resultModel;
        }
        //endregion

        QueryWrapper<YhKhjbxx> queryWrapper = new QueryWrapper<YhKhjbxx>();

        queryWrapper.eq("customer_no", customerNo).notIn("customer_state", '3', '4');
        List<YhKhjbxx> daCustomerList = daCustomerService.list(queryWrapper);
        if (daCustomerList.size() == 0) {
            resultModel.setCode(1);
            resultModel.setMessage("客户名称不存在！");
            return resultModel;
        }

        QueryWrapper<WxUser> queryWrapperWx = new QueryWrapper<WxUser>();
        queryWrapperWx.eq("customer_no", customerNo).eq("openid", openid);
        List<WxUser> wxUserList = wxUserService.list(queryWrapperWx);
        if (wxUserList.size() > 0) {
            if (!StringUtils.isBlank(wxUserList.get(0).getBindFlag()) && wxUserList.get(0).getBindFlag() == "1") {
                resultModel.setCode(3);
                resultModel.setMessage("该收费号已绑定微信！");
                return resultModel;
            }
        }

        //region 业务处理
        queryWrapper.clear();
        queryWrapper.eq("customer_no", customerNo);
        daCustomerList = daCustomerService.list(queryWrapper);
        WxUser wxUser = new WxUser();
        if (wxUserList.size() > 0) {

            wxUser = wxUserList.get(0);
            wxUser.setBindFlag("1");
            wxUser.setName(daCustomerList.get(0).getYhxm());
            wxUser.setAddress(daCustomerList.get(0).getYhdz());
            wxUser.setPhone(daCustomerList.get(0).getLxdh());
            wxUser.setBindDate(new Date());
            if (wxUserService.update(wxUser, queryWrapperWx)) {
                resultModel.setCode(0);
                resultModel.setMessage("绑定成功！");
            } else {
                resultModel.setCode(-1);
                resultModel.setMessage("绑定失败！");
            }
        } else {
            wxUser.setCustomerNo(customerNo);
            wxUser.setOpenid(openid);
            wxUser.setBindFlag("1");
            wxUser.setName(daCustomerList.get(0).getYhxm());
            wxUser.setAddress(daCustomerList.get(0).getYhdz());
            wxUser.setPhone(daCustomerList.get(0).getLxdh());
            wxUser.setBindDate(new Date());
            if (wxUserService.save(wxUser)) {
                resultModel.setCode(0);
                resultModel.setMessage("绑定成功！");
            } else {
                resultModel.setCode(-1);
                resultModel.setMessage("绑定失败！");
            }
        }

        //endregion
        return resultModel;
    }

    @PostMapping("/removeCustomer")
    @ApiOperation(value = "刪除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ResultRespone deleteCustomer(@RequestParam("openid") String openid, @RequestParam("customerNo") String customerNo) throws IOException {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);
        //region 参数验证
        //验证
        if (StringUtils.isBlank(customerNo)) {
            resultModel.setCode(-1);
            resultModel.setMessage("客户编号不能为空！");
            return resultModel;
        }
        if (StringUtils.isBlank(openid)) {
            resultModel.setCode(-1);
            resultModel.setMessage("微信openid不能为空！");
            return resultModel;
        }
        //endregion


        QueryWrapper<WxUser> queryWrapperWx = new QueryWrapper<WxUser>();
        queryWrapperWx.eq("customer_no", customerNo).eq("openid", openid);
        List<WxUser> wxUserList = wxUserService.list(queryWrapperWx);
        WxUser wxUser = new WxUser();
        WxUserLs wxUserLs = new WxUserLs();
        if (wxUserList.size() > 0) {
            wxUser = wxUserList.get(0);
            wxUserLs.setCustomerNo(wxUser.getCustomerNo());
            wxUserLs.setName(wxUser.getName());
            wxUserLs.setAddress(wxUser.getAddress());
            wxUserLs.setPhone(wxUser.getPhone());
            wxUserLs.setBindFlag(wxUser.getBindFlag());
            wxUserLs.setOpenid(wxUser.getOpenid());
            wxUserLs.setBindDate(wxUser.getBindDate());
            wxUserLs.setBindRemark(wxUser.getBindRemark());
            wxUserLs.setBindWxname(wxUser.getBindWxname());
            wxUserLs.setBindSex(wxUser.getBindSex());
            wxUserLs.setUnbindDate(new Date());
            wxUserLs.setUnbindName("本人");
            wxUserLs.setUnbindRemark("自愿");
            boolean saveWxCustomerHis = wxUserLsService.save(wxUserLs);
            wxUser.setBindFlag("0");
            wxUser.setUnbindDate(new Date());
            wxUser.setUnbindName("本人");
            wxUser.setUnbindRemark("自愿");
            boolean updatewxCustomer = wxUserService.update(wxUser, queryWrapperWx);
            if (saveWxCustomerHis && updatewxCustomer) {
                resultModel.setCode(0);
                resultModel.setMessage("解绑成功！");
            } else {
                resultModel.setCode(-1);
                resultModel.setMessage("解绑失败！");
            }

        } else {
            resultModel.setCode(-1);
            resultModel.setMessage("解绑失败！");
        }

        //region 业务处理


        //endregion
        return resultModel;
    }

    @GetMapping("/getBindCustomer")
    @ApiOperation(value = "获取绑定用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "微信openid", required = true, dataType = "String")
    })
    public ResultRespone getBindCustomer(@RequestParam("openid") String openid) {

        ResultRespone<Object> resultModel = new ResultRespone<>();
        resultModel.setCode(0);

        //region 参数验证
        //验证
        if (StringUtils.isBlank(openid)) {
            resultModel.setCode(-1);
            resultModel.setMessage("传入微信编号不能为空！");
            return resultModel;
        }

        //endregion

        QueryWrapper<WxUser> queryWrapperWx = new QueryWrapper<WxUser>();
        queryWrapperWx.eq("openid", openid).eq("bind_flag", "1").orderByAsc("customer_no");
        List<WxUser> wxUserList = wxUserService.list(queryWrapperWx);
        if (wxUserList.size() == 0) {
            resultModel.setCode(1);
            resultModel.setMessage("请绑定用户！");
            return resultModel;
        }


        //region 业务处理

        resultModel.setResult(wxUserList);
        resultModel.setMessage("成功！");
        //endregion
        return resultModel;
    }


}
