package com.iformall.controller.market;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.exception.MallinkException;
import com.iformall.utils.PayUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;

import com.iformall.domain.po.WxSubsidy;
import com.iformall.service.WxSubsidyService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Map;

@RestController
@RequestMapping("wxSubsidy")
@Api(description = "商城补贴支付接口")
public class WxSubsidyController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxSubsidyService wxSubsidyService;

    @ApiOperation("补贴扫码支付发起")
    @GetMapping("prePay")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "amount", value = "金额", dataType = "String", paramType = "query", required = true)})
    @SystemControllerLog(description = "商城补贴-补贴扫码支付发起")
    public void subsidyPrepay(String amount, HttpServletResponse response) throws Exception {
        String ipStr = getIpAddr();
        logger.info("subsidyPrepay: " + ipStr + "-" + amount);
        MallUserInfo userInfo = getUser();
        ResultData resultData = wxSubsidyService.createSubsidy(userInfo, ipStr, amount);
        if (resultData.code == 200) {
            String codeUrl = ((Map<String, String>) resultData.data).get("code_url");
            BufferedImage image = PayUtil.getQRCodeImge(codeUrl);

            response.setContentType("image/jpeg");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setIntHeader("Expires", -1);
            ImageIO.write(image, "JPEG", response.getOutputStream());
        } else {
            throw new MallinkException(resultData.code, resultData.message);
        }
    }

    @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 WxSubsidy wxSubsidy, Integer pageNum, Integer pageSize) {
        if (null == wxSubsidy) wxSubsidy = new WxSubsidy();
        final PageInfo<WxSubsidy> page = wxSubsidyService.listAsPage(wxSubsidy, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    @SystemControllerLog(description = "商城补贴-新增接口")
    public ResultData add(@RequestBody WxSubsidy wxSubsidy) {
        //Assert.notNull(wxSubsidy.getName(), "角色名不能为空");
        //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
        wxSubsidyService.saveOrUpdate(wxSubsidy);
        return new ResultData();
    }

    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    @SystemControllerLog(description = "商城补贴-更新")
    public ResultData update(@RequestBody WxSubsidy wxSubsidy) {
        wxSubsidyService.saveOrUpdate(wxSubsidy);
        return new ResultData();
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "商城补贴-删除")
    public ResultData delete(Long id) {
        wxSubsidyService.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) {
        return new ResultData(Result.SUCCESS, "查询成功", wxSubsidyService.getById(id));
    }


}
