package com.iformall.controller.basic;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
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.vo.WxMerchantVo;
import com.iformall.service.WxMerchantService;
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;

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

    @Autowired
    private WxMerchantService wxMerchantService;

    @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.setTenantId(getTenantId());
        wxMerchant.setSortColumns(BaseEntity.SortField.TopTime_DESC);
        final PageInfo<WxMerchant> page = wxMerchantService.listAsPage(wxMerchant, pageNum, pageSize);
        return new ResultData(page);
    }

    @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.setTenantId(getTenantId());
        wxMerchant.setSortColumns(BaseEntity.SortField.TopTime_DESC);
        final PageInfo<WxMerchant> page = wxMerchantService.listVoAsPage(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.setTenantId(getTenantId());
        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.setTenantId(getTenantId());
        return wxMerchantService.addMerchant(wxMerchant, getUserId());
    }

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

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

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

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

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

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

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

    @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);
    }

    @ApiOperation("商户数据导出")
    @GetMapping("/exportData")
    @SystemControllerLog(description = "商户-商户数据导出")
    public void exportData(@ModelAttribute WxMerchant wxMerchant, HttpServletRequest request, HttpServletResponse response) {
        wxMerchant.setTenantId(getTenantId());
        wxMerchantService.exportData(wxMerchant,request,response);
    }

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

}
