package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.service.WxAppinfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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.Map;

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

    @Autowired
    private WxAppinfoService wxAppinfoService;

    @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 WxAppinfo wxAppinfo, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxAppinfoController::list");
        if (null == wxAppinfo) wxAppinfo = new WxAppinfo();
        final PageInfo<WxAppinfo> page = wxAppinfoService.listAsPage(wxAppinfo, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    public ResultData add(@RequestBody WxAppinfo wxAppinfo) {
        logger.debug("[" + getIpAddr() + "] WxAppinfoController::add");
        //Assert.notNull(wxAppinfo.getName(), "角色名不能为空");
        //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
        wxAppinfoService.saveOrUpdate(wxAppinfo);
        return new ResultData();
    }

    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    public ResultData update(@RequestBody WxAppinfo wxAppinfo) {
        logger.debug("[" + getIpAddr() + "] WxAppinfoController::update");
        wxAppinfoService.saveOrUpdate(wxAppinfo);
        return new ResultData();
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxAppinfoController::delete");
        wxAppinfoService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

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


    @ApiOperation("C端更新AccessToken, 无限制二维码生成需要更新token")
    @GetMapping("/accessTokenUpdate")
    public ResultData ctokenUpdate() {
        logger.debug("[" + getIpAddr() + "] WxAppinfoController::ctokenUpdate");
        try {
            wxAppinfoService.ctokenUpdate(getTenantId());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        return new ResultData();
    }

    @ApiOperation(value = "下载二维码", notes = "参数{\"pageUrl\":\"String\", \"sceneParam\":\"二维码参数\", \"type\":0:有限二维码，1：无限二维码,\"withText\":int(0:不带字, 1:加一行字，2:加两行字),\"text1\":\"String\",\"text2\":\"String\"}")
    @PostMapping("/downQrCode")
    public ResultData downQrCode(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> params) {
        logger.debug("[" + getIpAddr() + "] WxAppinfoController::downQrCode");
        String pageUrl = (String) params.get("pageUrl");
        String sceneParam = (String) params.get("sceneParam");
        int type = 0;
        try {
            type = (int) params.get("type");
        } catch (Exception e) {
            type = 0;
        }

        int withText = 0;
        try {
            withText = (int) params.get("withText");
        } catch (Exception e) {
            withText = 0;
        }
        String text1 = (String) params.get("text1");
        String text2 = (String) params.get("text2");
        if (StringUtils.isBlank(pageUrl)) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "pageUrl不能为空");
        }
        try {
            wxAppinfoService.exportQrcode(request, response,
                    getTenantId(), type, pageUrl, sceneParam,
                    withText, text1, text2);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        return new ResultData();
    }


}
