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

import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.app.AntiFake;
import cn.wisenergy.model.dto.AntiFakeQuery;
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.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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

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

    @Autowired
    private AntiFakeService antiFakeService;

    @ApiOperation(value = "解析二维码", notes = "解析二维码", httpMethod = "POST")
    @PostMapping(value = "/admin/decode", headers = "content-type=multipart/form-data")
    public R<String> getByUserId(@RequestParam("file") MultipartFile file,
                                 HttpServletRequest request) throws Exception {
        return antiFakeService.decode(file);
    }

    @ApiOperation(value = "解析二维码", notes = "解析二维码", httpMethod = "POST")
    @PostMapping(value = "/admin/create/code", headers = "content-type=multipart/form-data")
    public R<Boolean> create(Integer number) throws Exception {
        return antiFakeService.createCode(number);
    }

    @ApiOperation(value = "扫描防伪二维码", notes = "扫描防伪二维码", httpMethod = "POST")
    @ApiImplicitParam(name = "productNo", value = "产品编号", dataType = "String")
    @PostMapping("/admin/scanCode")
    public R<String> scanCode(String productNo) {
        log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.productNo:" + productNo);
        if (StringUtils.isBlank(productNo)) {
            return R.error("入参为空!");
        }

        return antiFakeService.scanCode(productNo);
    }


    @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);
    }

}
