package cn.wisenergy.web.admin.controller.app;

import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.app.AntiFake;
import cn.wisenergy.model.app.ProductInfo;
import cn.wisenergy.model.dto.AntiFakeQuery;
import cn.wisenergy.model.dto.CreateCodeVo;
import cn.wisenergy.model.dto.ProductDto;
import cn.wisenergy.service.app.AntiFakeService;
import com.github.pagehelper.PageInfo;
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.apache.commons.lang3.StringUtils;
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.ParseException;

/**
 * @author 86187
 */
@Api(tags = "管理端-防伪二维码管理")
@RestController
@Slf4j
public class AntiFakeController {

    @Autowired
    private AntiFakeService antiFakeService;

    @ApiOperation(value = "创建二维码", notes = "创建二维码", httpMethod = "POST")
    @ApiImplicitParam(name = "codeVo", value = "二维码信息", dataType = "CreateCodeVo")
    @PostMapping(value = "/admin/create/code")
    public R<Boolean> create(@RequestBody CreateCodeVo codeVo) throws ParseException {
        return antiFakeService.createCode(codeVo);
    }

    @ApiOperation(value = "扫描防伪二维码", notes = "扫描防伪二维码", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String", required = true),
            @ApiImplicitParam(name = "securityCode", value = "安全码", dataType = "String", required = true)
    })
    @ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String")
    @GetMapping("/admin/scanCode")
    public R<String> scanCode(String shaValue, String securityCode) {
        log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.shaValue:" + shaValue, securityCode);
        if (StringUtils.isBlank(shaValue)) {
            return R.error("入参为空!");
        }

        return antiFakeService.scanCode(shaValue, securityCode);
    }


    @ApiOperation(value = "获取防伪二维码分页列表", notes = "获取防伪二维码分页列表", httpMethod = "GET")
    @ApiImplicitParam(name = "query", value = "查询条件", dataType = "AccountInfoQuery")
    @GetMapping("admin/code/getList")
    public R<PageInfo<AntiFake>> getList(AntiFakeQuery query) {
        log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.query:" + query);
        if (null == query) {
            return R.error("入参为空!");
        }

        return antiFakeService.getList(query);
    }

    @ApiOperation(value = "导出二维码", notes = "获取防伪二维码分页列表", httpMethod = "GET")
    @ApiImplicitParam(name = "batchNumber", value = "批次号", dataType = "String")
    @GetMapping("admin/report")
    public R<Boolean> report(String batchNumber,HttpServletRequest request, HttpServletResponse response) {
        log.info("shop-mall[]AntiFakeController[]report[]input.param.batchNumber:" + batchNumber);
        if (StringUtils.isBlank(batchNumber)) {
            return R.error("入参为空!");
        }

        return antiFakeService.report(batchNumber,request, response);
    }

    @ApiOperation(value = "获取产品信息", notes = "获取产品信息", httpMethod = "GET")
    @ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String", required = true)
    @GetMapping("admin/productInfo")
    public R<ProductDto> getProductInfo(String shaValue) {
        log.info("shop-mall[]AntiFakeController[]getProductInfo[]input.param.shaValue:" + shaValue);
        if (StringUtils.isBlank(shaValue)) {
            return R.error("入参为空!");
        }

        return antiFakeService.getProductInfo(shaValue);
    }

    @ApiOperation(value = "测试生成二维码图片", notes = "测试生成二维码图片", httpMethod = "GET")
    @ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String", required = true)
    @GetMapping("admin/ceshi")
    public R<Boolean> ceshi(String shaValue, HttpServletRequest request) throws Exception {
        log.info("shop-mall[]AntiFakeController[]ceshi[]input.param.shaValue:" + shaValue);
        if (StringUtils.isBlank(shaValue)) {
            return R.error("入参为空!");
        }

        return antiFakeService.ceishi(shaValue,request);
    }

}
