package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCUserBasicInfo;
import com.iformall.domain.po.WxCreditHistory;
import com.iformall.domain.vo.WxCreditHistoryVo;
import com.iformall.enums.EnumUserType;
import com.iformall.service.WxCUserBasicInfoService;
import com.iformall.service.WxCreditHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/credit")
@Api(description = "积分相关接口")
@Slf4j
public class WxCreditHistoryController extends BaseController  {

    @Autowired
    private WxCreditHistoryService wxCreditHistoryService;

    @Autowired
    private WxCUserBasicInfoService wxCUserBasicInfoService;

    @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) {
        log.debug("[" + getIpAddr() + "] WxCreditHistoryController::findByMerchantIdAndSpend");
        return new ResultData(wxCreditHistoryService.findByMerchantIdAndSpend(merchantId,spendStr,getTenantId()));
    }

    @ApiOperation("根据手机号查询接口")
    @GetMapping("/findByPhone")
    @ApiImplicitParam(name = "phone", value = "phone", dataType = "String", paramType = "query", required = true)
    public ResultData findByPhone(String phone) {
        log.debug("[" + getIpAddr() + "] WxCUserBasicInfoController::findByPhone");
        List<WxCUserBasicInfo> wxCUserBasicInfoList = wxCUserBasicInfoService.findByPhone(getTenantId(),phone);
        WxCUserBasicInfo wxCUserBasicInfo = new WxCUserBasicInfo();
        if (wxCUserBasicInfoList != null && wxCUserBasicInfoList.size() > 0) {
            wxCUserBasicInfo = wxCUserBasicInfoList.get(0);
        }
        return new ResultData(wxCUserBasicInfo);
    }

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

    @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) {
        log.debug("[" + getIpAddr() + "] WxCreditHistoryController::list");
        if (null == wxCreditHistory) wxCreditHistory = new WxCreditHistory();
        final PageInfo<WxCreditHistoryVo> page = wxCreditHistoryService.listAsPageMore(wxCreditHistory, pageNum, pageSize);
        return new ResultData(page);
    }
}
