package com.iformall.controller.datatower;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.annotation.UserDataRuleAnnotation;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxMerchantTradeDaily;
import com.iformall.domain.vo.WxMerchantTradeDailyVo;
import com.iformall.service.WxMerchantTradeDailyService;
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.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("wxMerchantTradeDaily")
public class WxMerchantTradeDailyController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMerchantTradeDailyService wxMerchantTradeDailyService;


    @UserDataRuleAnnotation("merchant_trade")
    @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 WxMerchantTradeDaily wxMerchantTradeDaily, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMerchantTradeDailyController::listYesterday");

        if (wxMerchantTradeDaily == null)
            wxMerchantTradeDaily = new WxMerchantTradeDaily();
        wxMerchantTradeDaily.setTenantId(getTenantId());

        wxMerchantTradeDaily.setSortColumns(BaseEntity.SortField.ReportDate_DESC,BaseEntity.SortField.MerchantId_DESC);
        PageInfo<WxMerchantTradeDailyVo> page = wxMerchantTradeDailyService.listAsPageVo(wxMerchantTradeDaily, pageNum, pageSize);

        return new ResultData(page);
    }

    @ApiOperation("数据汇总")
    @GetMapping("/summary")
    @SystemControllerLog(description = "商户解单-数据汇总")
    public ResultData summary(@ModelAttribute WxMerchantTradeDaily wxMerchantTradeDaily) {
        logger.debug("[" + getIpAddr() + "] WxMerchantTradeDailyController::summary");
        return wxMerchantTradeDailyService.summary(wxMerchantTradeDaily);
    }

    @ApiOperation("获商户得近30日解单")
    @GetMapping("/detail")
    @SystemControllerLog(description = "商户-获商户得近30日解单detail")
    public ResultData detail(Long merchantId) {
        logger.debug("[" + getIpAddr() + "] WxMerchantTradeDailyController::detail");
        return wxMerchantTradeDailyService.getVolumeOfMonth(merchantId);
    }

    @ApiOperation("获得商场近30日解单")
    @GetMapping("/listMonth")
    @SystemControllerLog(description = "商户-获得商场近30日解单")
    public ResultData listMonth() {
        logger.debug("[" + getIpAddr() + "] WxMerchantTradeDailyController::listMonth");
        return wxMerchantTradeDailyService.getVolumeTotalOfMonth(getTenantId());
    }

    @ApiOperation("更新解单数据")
    @PostMapping("/update")
    @SystemControllerLog(description = "商户-更新解单数据")
    public ResultData update(@RequestBody WxMerchantTradeDaily wxMerchantTradeDaily) {
        logger.debug("[" + getIpAddr() + "] WxMerchantTradeDailyController::updateYesterday");
        if (wxMerchantTradeDaily == null ||
                wxMerchantTradeDaily.getMerchantId() == null ||
                wxMerchantTradeDaily.getTradeAmt() == null)
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        wxMerchantTradeDaily.setTenantId(getTenantId());
        if (wxMerchantTradeDaily.getReportDate() == null) {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            wxMerchantTradeDaily.setReportDate(fmt.format(new Date()));
        }
        return wxMerchantTradeDailyService.update(wxMerchantTradeDaily);
    }

    @ApiOperation("查询解单数据")
    @GetMapping("/queryTradeDaily")
    @SystemControllerLog(description = "商户-查询解单数据")
    public ResultData queryTradeDaily(@ModelAttribute WxMerchantTradeDaily wxMerchantTradeDaily) {
        logger.debug("[" + getIpAddr() + "] WxMerchantTradeDailyController::queryTradeDaily");
        wxMerchantTradeDaily.setTenantId(getTenantId());
        return wxMerchantTradeDailyService.queryTradeDaily(wxMerchantTradeDaily);
    }

    @UserDataRuleAnnotation("merchant_trade")
    @GetMapping("exportData")
    @SystemControllerLog(description = "商户解单数据-导出数据")
    public void exportData(@ModelAttribute WxMerchantTradeDaily wxMerchantTradeDaily, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxMerchantTradeDailyController::exportData");
        if (null == wxMerchantTradeDaily) {
            wxMerchantTradeDaily = new WxMerchantTradeDaily();
        }
        wxMerchantTradeDaily.setTenantId(getTenantId());
        wxMerchantTradeDaily.setSortColumns(BaseEntity.SortField.ReportDate_DESC,BaseEntity.SortField.MerchantId_DESC);
        wxMerchantTradeDailyService.exportData(wxMerchantTradeDaily, request, response);
    }

}
