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

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.diversionrecord.vo.*;
import cn.iocoder.yudao.module.identity.convert.diversionrecord.DiversionRecordConvert;
import cn.iocoder.yudao.module.identity.dal.dataobject.code.CodeDO;
import cn.iocoder.yudao.module.identity.dal.dataobject.diversionrecord.DiversionRecordDO;
import cn.iocoder.yudao.module.identity.service.diversionrecord.DiversionRecordService;
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.Collection;
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/diversion-record")
@Validated
public class DiversionRecordController {

    @Resource
    private DiversionRecordService diversionRecordService;


    @PutMapping("/update")
    @ApiOperation("更新疑似窜货记录如果是窜货那就新增窜货数据")
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:update')")
    public CommonResult<Boolean> updateDiversionRecord(@Valid @RequestBody DiversionRecordUpdateReqVO updateReqVO) {
        diversionRecordService.updateDiversionRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除疑似窜货记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:delete')")
    public CommonResult<Boolean> deleteDiversionRecord(@RequestParam("id") Long id) {
        diversionRecordService.deleteDiversionRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得疑似窜货记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:query')")
    public CommonResult<DiversionRecordRespVO> getDiversionRecord(@RequestParam("id") Long id) {
        DiversionRecordDO diversionRecord = diversionRecordService.getDiversionRecord(id);
        return success(DiversionRecordConvert.INSTANCE.convert(diversionRecord));
    }

    @GetMapping("/list")
    @ApiOperation("获得疑似窜货记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:query')")
    public CommonResult<List<DiversionRecordRespVO>> getDiversionRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<DiversionRecordDO> list = diversionRecordService.getDiversionRecordList(ids);
        return success(DiversionRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得疑似窜货记录分页")
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:query')")
    public CommonResult<PageResult<DiversionRecordRespVO>> getDiversionRecordPage(@Valid DiversionRecordPageReqVO pageVO) {
        PageResult<DiversionRecordDO> pageResult = diversionRecordService.getDiversionRecordPage(pageVO);
        return success(DiversionRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出疑似窜货记录 Excel")
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:export')")
    @OperateLog(type = EXPORT)
    public void exportDiversionRecordExcel(@Valid DiversionRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DiversionRecordDO> list = diversionRecordService.getDiversionRecordList(exportReqVO);
        // 导出 Excel
        List<DiversionRecordExcelVO> datas = DiversionRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "疑似窜货记录.xls", "数据", DiversionRecordExcelVO.class, datas);
    }
//    {  "belongBusinessId": 1,  "belongBusinessName": "测试商户1",  "businessCode": "",  "commonSaleArea": "山西省/阳泉市/郊区",  "commonSaleAreaId": 140311,  "contactName": "张三",  "contactPhone": "13768952525",  "createTime": "2023-04-12 09:53:40",  "creator": "1",  "dealerId": "1",  "dealerName": "1",  "deleted": 0,  "id": 1,  "remarks": "",  "updateTime": "2023-05-23 16:06:34",  "updater": "1"}
//    {  "belongBusinessId": 1,  "belongBusinessName": "测试商户1",  "blockChainBlockIdentity": "8idad2saek42fxssdotao49oqltijlapkw9p3svxbrqbhmzr",  "blockChainId": "COCD202305251516159IXMXIBC",  "blockState": 2,  "createTime": "2023-04-24 17:14:07",  "creator": "1",  "deleted": 0,  "goodsId": "GOCD202304131028TSOC",  "goodsName": "大西瓜",  "goodsNum": 0,  "harvestBatchId": "HABTBC20230424165426A32RGFEY",  "harvestDate": "2023-04-25 01:14:07",  "harvestId": "INPJOE20230424165542OA3G9JYS",  "id": 1,  "remarks": "666",  "templateId": "",  "templateName": "",  "updateTime": "2023-05-29 15:18:06",  "updater": "1"}
    @PostMapping("/generateDiversionRecord")
    @ApiOperation("创建疑似窜货记录")
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:create')")
    public void generateDiversionRecord(@RequestBody CodeDO codeDO,
                                        @RequestParam ("scanLocation") String scanLocation) {

       diversionRecordService.generateDiversionRecord( codeDO,scanLocation);
    }

    @GetMapping("/getScanLocations")
    @ApiOperation("根据商品编码查询所有数据 然后在查询出所有的数据的同时再过滤出所有相对应的扫码地区")
    @PreAuthorize("@ss.hasPermission('identity:diversion-record:query')")
    public CommonResult<List<DiversionRecordDO>> getScanLocationsAndTimesByGoodsId(@RequestParam("goodsId") String goodsId,
                                                                                   @RequestParam("batchId") String batchId,
                                                                                   @RequestParam("scanLocation") String scanLocation)  {
        List<DiversionRecordDO> scanLocationsAndTimesByGoodsId = diversionRecordService.getScanLocationsAndTimesByGoodsId(goodsId,batchId,scanLocation);
        return success(scanLocationsAndTimesByGoodsId);
    }

    @GetMapping("/getConsistentGoodsIdAndScanLocations")
    @ApiOperation("查询出所有一致的商品编码和扫码地区")
    public CommonResult<List<String>> getConsistentGoodsIdAndScanLocations(@RequestBody List<DiversionRecordDO> records,
                                                                                @RequestParam("batchId") String batchId,
                                                                                @RequestParam("goodsId") String goodsId)  {
        List<String> consistentGoodsIdAndScanLocations = diversionRecordService.getConsistentGoodsIdAndScanLocations(records, batchId, goodsId);
        return success(consistentGoodsIdAndScanLocations);
    }
}
