package com.iformall.controller.basic;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.annotation.UserDataRuleAnnotation;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.po.WxProfitSharingReceiver;
import com.iformall.domain.vo.WxMerchantTradeDetailVo;
import com.iformall.domain.vo.WxMerchantTradeVo;
import com.iformall.domain.vo.WxMerchantVo;
import com.iformall.service.QrCodeService;
import com.iformall.service.WxMerchantService;
import com.iformall.service.WxScoreRulesService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxMerchant")
public class WxMerchantController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMerchantService wxMerchantService;
    @Autowired
    private QrCodeService qrCodeService;
    @Autowired
    private WxScoreRulesService wxScoreRulesService;

    @UserDataRuleAnnotation("merchant_list")
    @ApiOperation("分页列表接口")
    @GetMapping("listVo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "商户-列表")
    public ResultData listVo(@ModelAttribute WxMerchant wxMerchant, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::list");
        if (null == wxMerchant) wxMerchant = new WxMerchant();
        wxMerchant.updateTenantInfo(getTenantInfo());
        wxMerchant.setSortColumns(BaseEntity.SortField.TopTime_DESC);
        final PageInfo<WxMerchant> page = wxMerchantService.listVoAsPage(wxMerchant, pageNum, pageSize);
        return new ResultData(page);
    }


    @ApiOperation("分页列表接口")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
    })
    @SystemControllerLog(description = "商户-列表")
    public ResultData list(@ModelAttribute WxMerchant wxMerchant, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::list");
        if (null == wxMerchant) wxMerchant = new WxMerchant();
        wxMerchant.updateTenantInfo(getTenantInfo());
        wxMerchant.setSortColumns(BaseEntity.SortField.TopTime_DESC);
        final PageInfo<WxMerchant> page = wxMerchantService.listAsPage(wxMerchant, pageNum, pageSize);
        return new ResultData(page);
    }


    @ApiOperation("ETCP商户列表")
    @GetMapping("etcplist")
    @SystemControllerLog(description = "商户-ETCP商户列表")
    public ResultData etcpList(@ModelAttribute WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::etcpList");
        if (null == wxMerchant) wxMerchant = new WxMerchant();
        wxMerchant.updateTenantInfo(getTenantInfo());
        final List<WxMerchant> merchantList = wxMerchantService.etcpList(wxMerchant);
        return new ResultData(merchantList);
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "商户-删除")
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::delete");
        wxMerchantService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "商户-查询")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::findById");
        WxMerchant wxMerchant = wxMerchantService.getById(id);
        return new ResultData(wxMerchant);
    }

    @ApiOperation("停用")
    @GetMapping("disable")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "商户-停用")
    public ResultData disable(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::disable");
        wxMerchantService.disable(id);
        return new ResultData(Result.SUCCESS, "停用成功");
    }

    @ApiOperation("新增商户接口")
    @PostMapping("addMerchant")
    @SystemControllerLog(description = "商户-新增")
    public ResultData addMerchant(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::addMerchant");
        wxMerchant.updateTenantInfo(getTenantInfo());

        ResultData resultData = wxMerchantService.addMerchant(wxMerchant, getUserId());

        if(resultData.code == 200){
            //加载二维码图片
            String pageUrl = "pages/index/index";
            String param = "t:md:"+resultData.data;
            ResultData resultQrCode = qrCodeService.uploadQrcode(wxMerchant,1,pageUrl,param,0,"","","店铺详情");
            Map<String,String> map = (Map)resultQrCode.data;
            if(map!=null && map.get("url") !=null) {
                String url = map.get("url");
                wxMerchant.setQrCode(url);
                wxMerchantService.updateMerchant(wxMerchant);
            }
        }

        return resultData;
    }

    @ApiOperation("更新商户接口")
    @PostMapping("updateMerchant")
    @SystemControllerLog(description = "商户-更新")
    public ResultData updateMerchant(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::updateMerchant");
        wxMerchant.updateTenantInfo(getTenantInfo());
        return wxMerchantService.updateMerchant(wxMerchant);
    }

    @ApiOperation("更新账户接口")
    @PostMapping("updateMerchantAccount")
    @SystemControllerLog(description = "商户-更新账户")
    public ResultData updateMerchantAccount(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::updateMerchantAccount");
        wxMerchant.updateTenantInfo(getTenantInfo());
        return wxMerchantService.updateMerchantAccount(wxMerchant);
    }

    @ApiOperation("更新管理员接口")
    @PostMapping("updateMerchantAdmin")
    @SystemControllerLog(description = "商户-更新管理员")
    public ResultData updateMerchantAdmin(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::updateMerchantAdmin");
        wxMerchant.updateTenantInfo(getTenantInfo());
        return wxMerchantService.updateMerchantAdmin(wxMerchant);
    }

    @ApiOperation("更新法人接口")
    @PostMapping("updateMerchantCorp")
    @SystemControllerLog(description = "商户-更新法人")
    public ResultData updateMerchantCopr(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::updateMerchantCorp");
        wxMerchant.updateTenantInfo(getTenantInfo());
        return wxMerchantService.updateMerchantCorp(wxMerchant);
    }

    @ApiOperation("更新税务接口")
    @PostMapping("updateMerchantTax")
    @SystemControllerLog(description = "商户-更新税务")
    public ResultData updateMerchantTax(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::updateMerchantTax");
        wxMerchant.updateTenantInfo(getTenantInfo());
        return wxMerchantService.updateMerchantTax(wxMerchant);
    }

    @ApiOperation("商户信息设置接口")
    @PostMapping("updateMerchantLevel")
    @SystemControllerLog(description = "商户-更新会员等级权益")
    public ResultData updateMerchantLevel(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::updateMerchantLevel");
        wxMerchant.updateTenantInfo(getTenantInfo());
        return wxMerchantService.updateMerchantLevel(wxMerchant);
    }

    @ApiOperation("查询当前租户下商户名称列表")
    @GetMapping("/name_list")
    @SystemControllerLog(description = "查询当前租户下商户名称列表")
    public ResultData nameList() {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::name_list");
        return new ResultData(wxMerchantService.findList(getTenantInfo()));
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findMerchantById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "商户-查询")
    public ResultData findMerchantById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::findMerchantById");
        WxMerchantVo wxMerchant = wxMerchantService.findMerchantById(id);
        return new ResultData(wxMerchant);
    }

    @UserDataRuleAnnotation("merchant_list")
    @ApiOperation("商户数据导出")
    @GetMapping("/exportData")
    @SystemControllerLog(description = "商户-商户数据导出")
    public void exportData(@ModelAttribute WxMerchant wxMerchant, HttpServletRequest request, HttpServletResponse response) {
        wxMerchant.updateTenantInfo(getTenantInfo());
        wxMerchant.setSortColumns(BaseEntity.SortField.TopTime_DESC);
        wxMerchantService.exportData(wxMerchant,request,response);
    }

    @ApiOperation("置顶")
    @PostMapping("top")
    @SystemControllerLog(description = "商户-置顶")
    public ResultData top(@RequestBody WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::top");
        wxMerchant.updateTenantInfo(getTenantInfo());
        return wxMerchantService.top(wxMerchant);
    }

    @ApiOperation("更新收款账户状态")
    @PostMapping("useAccount")
    @SystemControllerLog(description = "商户-更新收款账户状态")
    public ResultData useAccount(@RequestBody WxProfitSharingReceiver wxProfitSharingReceiver) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::useAccount");
        wxProfitSharingReceiver.updateTenantInfo(getTenantInfo());
        return wxMerchantService.useAccount(wxProfitSharingReceiver);
    }

    @ApiOperation("查看是否存在商户名称相同记录")
    @GetMapping("/hasMerchant")
    @SystemControllerLog(description = "商户-查看是否存在商户名称相同记录")
    public ResultData hasMerchant(@ModelAttribute WxMerchant wxMerchant) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::hasMerchant");
        wxMerchant.updateTenantInfo(getTenantInfo());
        boolean has = wxMerchantService.hasMerchant(wxMerchant);
        return new ResultData(has);
    }

    @ApiOperation("商户会员消费分页列表接口")
    @GetMapping("userTradeList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "商户-消费列表")
    public ResultData userTradeList(@ModelAttribute WxMerchant wxMerchant, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::list");
        if (null == wxMerchant) wxMerchant = new WxMerchant();
        wxMerchant.updateTenantInfo(getTenantInfo());
        wxMerchant.setSortColumns(BaseEntity.SortField.TopTime_DESC);
        final PageInfo<WxMerchantTradeVo> page = wxMerchantService.userTradeList(wxMerchant, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("商户会员消费明细列表接口")
    @GetMapping("userTradeDetailList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "商户-商户会员消费明细列表接口")
    public ResultData userTradeDetailList(@ModelAttribute WxMerchant wxMerchant, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::list");
        if (null == wxMerchant) wxMerchant = new WxMerchant();
        wxMerchant.updateTenantInfo(getTenantInfo());
        final PageInfo<WxMerchantTradeDetailVo> page = wxMerchantService.userTradeDetailList(wxMerchant, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("导出商户会员消费分页列表")
    @GetMapping("exportUserTradeDetailList")
    @SystemControllerLog(description = "导出商户会员消费分页列表")
    public void exportUserTradeList(@ModelAttribute WxMerchant wxMerchant,HttpServletRequest request, HttpServletResponse response) throws Exception{
        logger.debug("[" + getIpAddr() + "] WxBusinessController::exportUserTradeDetailList");
        if (null == wxMerchant) wxMerchant = new WxMerchant();
        wxMerchant.updateTenantInfo(getTenantInfo());
        wxMerchantService.exportUserTradeList(wxMerchant, request,response);
    }

}
