package com.ruoyi.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.api.domain.InStoreDto;
import com.ruoyi.api.domain.OutStoreDto;
import com.ruoyi.api.domain.StockDto;
import com.ruoyi.api.service.impl.ApiService;
import com.ruoyi.common.core.domain.AjaxResult;

import static com.ruoyi.common.core.domain.AjaxResult.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.manage.domain.*;
import com.ruoyi.manage.service.IBaseLocationService;
import com.ruoyi.manage.service.IBaseManufacturerService;
import com.ruoyi.manage.service.IRecordInvalidatedService;
import com.ruoyi.manage.service.IRecordStoreCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * @author wanghao
 * @date 2024/1/11  8:55
 */
@RestController
@RequestMapping("/api")
public class ApiController {
    @Autowired
    private IBaseLocationService baseLocationService;
    @Autowired
    private IBaseManufacturerService baseManufacturerService;
    @Autowired
    private ApiService apiService;

    //入库
    // 查询库位 和 品牌
    @PostMapping("/instore/select")
    public AjaxResult instoreSelect() {
        BaseLocation baseLocation = new BaseLocation();
        baseLocation.setStoteTag(1L);
        baseLocation.setIsEnable("Y");
        List<BaseLocation> baseLocationList = baseLocationService.selectBaseLocationList(baseLocation);
        BaseManufacturer baseManufacturer = new BaseManufacturer();
        baseManufacturer.setIsEnable("Y");
        List<BaseManufacturer> baseManufacturerList = baseManufacturerService.selectBaseManufacturerList(baseManufacturer);
        Map<String, List> map = new HashMap<>();
        map.put("baseLocationList", baseLocationList);
        map.put("baseManufacturerList", baseManufacturerList);
        return success(map);
    }

    @PostMapping("/instore/submit")
    public AjaxResult instoreSubmit(@RequestBody InStoreDto inStore) {
        apiService.submintInsertRecordIn(inStore);
        apiService.submintInsertLedger(inStore);
        return success();
    }


    @PostMapping("/outStore/select")
    public AjaxResult outStoreSelect() {
        BaseLocation baseLocation = new BaseLocation();
        baseLocation.setStoteTag(2L);
        baseLocation.setIsEnable("Y");
        List<BaseLocation> baseLocationList = baseLocationService.selectBaseLocationList(baseLocation);
        return success(baseLocationList);
    }

    @PostMapping("/outStore/submit")
    public AjaxResult outStoreSubmit(@RequestBody OutStoreDto outStoreDto) {
        apiService.submitInsertRecordOut(outStoreDto);
        apiService.updataLedger(outStoreDto);
        return success();
    }

    @PostMapping("/storecheck/select")
    public AjaxResult storeCheckSelect() {
        List<StockDto> list = apiService.storeCheckSelect();
        if (list == null || list.isEmpty()) {
            return error("库存为空");
        }
        return success(list);
    }

    @Autowired
    private IRecordStoreCheckService checkService;

    @Transactional
    @PostMapping("/storecheck/submit")
    public AjaxResult storeCheckSubmit(String json, String user) {
        try {
            List<StockDto> list = JSONObject.parseArray(json, StockDto.class);
            RecordStoreCheck recordStoreCheck = new RecordStoreCheck();
            recordStoreCheck.setCheckNumber(list.size());
            recordStoreCheck.setCheckTime(DateUtils.getNowDate());
            recordStoreCheck.setCheckUser(user);
            int key = checkService.insertRecordStoreCheck(recordStoreCheck);
            apiService.insertCheckInfo(list, recordStoreCheck.getCheckId());
            return success();
        } catch (Exception e) {
            return error("数据异常,请求失败");
        }

    }


    @PostMapping("/bf/select")
    public AjaxResult bfSelect(String epc) {
        LedgerRfid ledgerRfid = apiService.bfSelect(epc);
        if (ledgerRfid == null) {
            return error("RFID 扫描不正确");
        }
        return success(ledgerRfid);
    }

    @Autowired
    private IRecordInvalidatedService recordInvalidatedService;

    @PostMapping("/bf/submit")
    public AjaxResult bfSubmit(String epc, String user, String remark) {
        //插入记录
        RecordInvalidated recordInvalidated = new RecordInvalidated();
        recordInvalidated.setEpc(epc);
        recordInvalidated.setCreateBy(user);
        recordInvalidated.setRemark(remark);
        recordInvalidatedService.insertRecordInvalidated(recordInvalidated);
        //修改台账
        apiService.updataLedgerForBf(epc, "2");
        return success();
    }

    @PostMapping("/xj/select")
    public AjaxResult xjSelect(String user) {
        List<LedgerRfid> list = apiService.xjSelect(user);
        if (list == null) {
            return error("没有维护巡检区域");
        }
        return success(list);
    }


    @PostMapping("/fpin/select")
    public AjaxResult fpInSelect(String epc) {
        LedgerRfid ledgerRfid = apiService.fpInSelect(epc);
        if (ledgerRfid == null) {
            return error("RFID 扫描不正确");
        }
        BaseLocation baseLocation = new BaseLocation();
        baseLocation.setStoteTag(1L);
        baseLocation.setIsEnable("Y");
        List<BaseLocation> baseLocationList = baseLocationService.selectBaseLocationList(baseLocation);
        ledgerRfid.setBaseLocationList(baseLocationList);
        return success(ledgerRfid);
    }

    @PostMapping("/fpin/submit")
    public AjaxResult fpInSubmit(String epc,String localtionCode,String user,String bf,String subCode) {
        int i=apiService.fpInSubmit(epc,localtionCode,user,bf,subCode);
        apiService.updataLedgerIn(epc,subCode);
        return success();
    }
}
