package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCreditHistory;
import com.iformall.domain.vo.WxCreditHistoryVo;
import com.iformall.service.WxCreditHistoryService;
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.*;

@RestController
@RequestMapping("wxCreditHistory")
public class WxCreditHistoryController extends BaseController
{
	@Autowired
    private WxCreditHistoryService wxCreditHistoryService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@ApiOperation("分页列表接口")
    @GetMapping("list")
        @ApiImplicitParams({
    	  @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
    	  @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)})
    public ResultData list(@ModelAttribute WxCreditHistory wxCreditHistory, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxCreditHistoryController::list");
        if (null == wxCreditHistory) wxCreditHistory = new WxCreditHistory();
        final PageInfo<WxCreditHistoryVo> page = wxCreditHistoryService.listAsPageMore(wxCreditHistory, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("根据商户ID和消费金额获取所增加积分 取data里credit的值")
    @GetMapping("findByMerchantIdAndSpend")
    @ApiImplicitParams({
            @ApiImplicitParam(name="merchantId",value="商户ID",dataType="long", paramType = "query",required=true),
            @ApiImplicitParam(name="spend",value="消费金额",dataType="int", paramType = "query",required=true)})
    public ResultData findByMerchantIdAndSpend(@RequestParam Long merchantId, @RequestParam String spendStr) {
        logger.debug("[" + getIpAddr() + "] WxCreditHistoryController::findByMerchantIdAndSpend");
        return new ResultData(wxCreditHistoryService.findByMerchantIdAndSpend(merchantId,spendStr,getTenantId()));
    }

	@ApiOperation("新增接口")
    @PostMapping("add")
    public ResultData add(@RequestBody WxCreditHistory wxCreditHistory) {
        logger.debug("[" + getIpAddr() + "] WxCreditHistoryController::add");
        wxCreditHistory.setTenantId(getTenantId());
        wxCreditHistory.setOperatorId(getUser().getId());
        WxCreditHistory credit = wxCreditHistoryService.saveOrUpdate(wxCreditHistory);
        return new ResultData(Result.SUCCESS,"操作成功",credit);
    }

     @ApiOperation("根据id查询接口")
     @GetMapping("/findById")
     @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
    public ResultData findById(Long id) {
         logger.debug("[" + getIpAddr() + "] WxCreditHistoryController::findById");
         return new ResultData(Result.SUCCESS,"查询成功",wxCreditHistoryService.getById(id));
    }

    /**
     * 根据A端设置的【每自然年末扣除前N年用户积分】规则,扣减用户积分
     * @return
     */
    public ResultData clearCreditByYear(){
        logger.debug("[" + getIpAddr() + "] WxCreditHistoryController::clearCreditByYear");
        wxCreditHistoryService.clearCreditByYear();
        return new ResultData(Result.SUCCESS);
    }

}
