package cn.iocoder.yudao.module.identity.controller.admin.code;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.identity.controller.admin.code.vo.*;
import cn.iocoder.yudao.module.identity.convert.code.CodeConvert;
import cn.iocoder.yudao.module.identity.dal.dataobject.code.CodeDO;
import cn.iocoder.yudao.module.identity.qrcode.QRCodeProperties;
import cn.iocoder.yudao.module.identity.service.code.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 标识码")
@RestController
@RequestMapping("/identity/identity-code")
@Validated
public class CodeController {

    @Resource
    private CodeService codeService;
    @Resource
    private QRCodeProperties qrCodeProperties;

    @PostMapping("/create")
    @ApiOperation("创建标识码组")
    @PreAuthorize("@ss.hasPermission('identity:code:create')")
    public CommonResult<Long[]> createCodes(@Valid @RequestBody CodeCreateReqVO createReqVO) {
        return success(codeService.createCode(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新标识码")
    @PreAuthorize("@ss.hasPermission('identity:code:update')")
    public CommonResult<Boolean> updateCode(@Valid @RequestBody CodeUpdateReqVO updateReqVO) {
        codeService.updateCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除标识码")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:code:delete')")
    public CommonResult<Boolean> deleteCode(@RequestParam("id") Long id) {
        codeService.deleteCode(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得标识码")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:code:query')")
    public CommonResult<CodeRespVO> getCode(@RequestParam("id") Long id) {
        CodeDO code = codeService.getCode(id);
        return success(CodeConvert.INSTANCE.convert(code));
    }

    @GetMapping("/list")
    @ApiOperation("获得标识码列表")
    @PreAuthorize("@ss.hasPermission('identity:code:query')")
    public CommonResult<List<CodeDO>> getCodeList() {
        List<CodeDO> codeList = codeService.getCodeList();
        return success(codeList);
    }

    @GetMapping("/page")
    @ApiOperation("获得标识码分页")
    @PreAuthorize("@ss.hasPermission('identity:code:query')")
    public CommonResult<PageResult<CodeRespVO>> getCodePage(@Valid CodePageReqVO pageVO) {
        PageResult<CodeDO> pageResult = codeService.getCodePage(pageVO);
        return success(CodeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出标识码 Excel")
    @PreAuthorize("@ss.hasPermission('identity:code:export')")
    @OperateLog(type = EXPORT)
    public void exportCodeExcel(@RequestParam("generateRecordId") String generateRecordId, HttpServletResponse response) throws IOException {

        List<CodeDO> list = codeService.getCodesByGenerateRecordId(generateRecordId);
        // 导出 Excel
        List<CodeExcelVO> datas = CodeConvert.INSTANCE.convertList02(list);
        //拼接getIdentityCode路径
        for (CodeExcelVO codeExcelVO : datas) {
            String identityCode = qrCodeProperties.getUrl() + codeExcelVO.getIdentityCode();
            codeExcelVO.setIdentityCode(identityCode);
        }
        ExcelUtils.write(response, "文字码包.xls", "数据", CodeExcelVO.class, datas);
    }

    @GetMapping("/export-third-excel")
    @ApiOperation("导出标识码 Excel")
    @PreAuthorize("@ss.hasPermission('identity:code:export')")
    @OperateLog(type = EXPORT)
    public void exportThirdCodeExcel(@RequestParam("generateRecordId") String generateRecordId, HttpServletResponse response) throws IOException {

        List<CodeDO> list = codeService.getCodesByGenerateRecordId(generateRecordId);
        // 导出 Excel
        List<ThirdCodeExcelVO> datas = CodeConvert.INSTANCE.convert2ThirdExcel(list);
        //拼接getIdentityCode路径
        for (ThirdCodeExcelVO codeExcelVO : datas) {
            String identityCode = qrCodeProperties.getUrl() + codeExcelVO.getIdentityCode() + "&m=";
            codeExcelVO.setIdentityCode(identityCode);
        }
        ExcelUtils.write(response, "第三方标签文字码包.xls", "数据", ThirdCodeExcelVO.class, datas);
    }

    @GetMapping("/getCodesByGenerateRecordId")
    @ApiOperation("根据指定的生码记录id获取所有标识码信息列表")
    @ApiImplicitParam(name = "generateRecordId", value = "生码记录id", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<List<CodeDO>> getCodesByGenerateRecordId(@RequestParam("generateRecordId") String generateRecordId) {
        List<CodeDO> byGenerateRecordId = codeService.getCodesByGenerateRecordId(generateRecordId);
        return success(byGenerateRecordId);
    }

    @GetMapping("/getCodesByIdentityCode")
    @ApiOperation("根据生码记录id和identityCode查询对应的标识码信息")
    @ApiImplicitParam(name = "generateRecordId", value = "生码记录id", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult <List<CodeDO>> getCodeByGenerateRecordIdAndIdentityCode(
                                                              @RequestParam("generateRecordId") String generateRecordId,
                                                              @RequestParam(value = "identityCode", required = false)String identityCode) {
        List<CodeDO> byBelongBusinessIdAndIdentityCode = codeService.getCodeByGenerateRecordIdAndIdentityCode(generateRecordId, identityCode);
        return success(byBelongBusinessIdAndIdentityCode);
    }

    @GetMapping("/getCodeDOInfoByGenerateRecordId")
    @ApiOperation("根据指定的生码记录id获取批次id和商品昵称")
    @ApiImplicitParam(name = "generateRecordId", value = "生码记录id", required = true, example = "CDREOE20230526154701L40G5PF0", dataTypeClass = Long.class)
    public CommonResult<CodeDO> getCodeDOInfoByGenerateRecordId(@RequestParam("generateRecordId") String generateRecordId) {
        CodeDO codeDOInfoByGenerateRecordId = codeService.getCodeDOInfoByGenerateRecordId(generateRecordId);
        return success(codeDOInfoByGenerateRecordId);
    }
}
