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.model.dto.CreateCodeVo;
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 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 = "POST")
    @ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String")
    @PostMapping("/admin/scanCode")
    public R<String> scanCode(String shaValue) {
        log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.shaValue:" + shaValue);
        if (StringUtils.isBlank(shaValue)) {
            return R.error("入参为空!");
        }

        return antiFakeService.scanCode(shaValue);
    }


    @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 = "ids", value = " 二维码主键id :\"1,2,3,4,5,6,7,8,9,\"", dataType = "String")
    @GetMapping("admin/report")
    public R<Boolean> report(String ids, HttpServletResponse response) {
        log.info("shop-mall[]AntiFakeController[]report[]input.param.ids:" + ids);
        if (StringUtils.isBlank(ids)) {
            return R.error("入参为空!");
        }

        return antiFakeService.report(ids, response);
    }

}
