package com.ruoyi.web.controller.app;

import com.ruoyi.app.domain.*;
import com.ruoyi.app.domain.DTO.PmsStockInCompleteReq;
import com.ruoyi.app.service.IAppPendingStorageService;
import com.ruoyi.app.service.IAppPmsOrderInService;
import com.ruoyi.app.service.IAppPmsOrderOutService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.OperatorType;
import com.ruoyi.common.utils.OrderCodeFactory;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.PmsOrderInRequest;
import com.ruoyi.web.domain.PmsOrderOutRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.QueryAnnotation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/app/pms")
public class AppPmsController extends BaseController {
    @Autowired
    private IAppPmsOrderInService appPmsOrderInService;// 入库单
    @Autowired
    private IAppPmsOrderOutService appPmsOrderOutService;// 出库单

    @Autowired
    private IAppPendingStorageService appPendingStorageService;

    private final List<Integer> orderInTypeList = Arrays.asList(1, 2, 3, 4, 5, 6);

    /**
     * Pms入库单请求
     */
    @ApiOperation("Pms入库单请求")
    @Log(title = "Pms入库单请求", skipAuth = true)
    @PostMapping("/orderIn")
    @Anonymous
    public AjaxResult pmsOrderIn(@RequestBody PmsOrderInRequest orderInRequest) {
        // TODO
        // 判断数据是否缺失
        Integer orderType = orderInRequest.getOrderType();
        if (StringUtils.isEmpty(orderInRequest.getListId())
                || !orderInTypeList.contains(orderInRequest.getOrderType())
                || StringUtils.isEmpty(orderInRequest.getOrderId())
                || StringUtils.isEmpty(orderInRequest.getGoodsId())
                || StringUtils.isEmpty(orderInRequest.getGoodsCode())
                || StringUtils.isEmpty(orderInRequest.getGoodsDesc())
                || orderInRequest.getGoodsNum() == null) {
            return error("缺少请求数据。");
        }
        // 判断入库单号是否重复
        AppPmsOrderIn existAppPmsOrderIn = appPmsOrderInService.selectAppPmsOrderInByListId(orderInRequest.getListId());
        if (existAppPmsOrderIn != null) {
           return AjaxResult.error("入库单已存在");
        }
        AppPmsOrderIn appPmsOrderIn = new AppPmsOrderIn();
        BeanUtils.copyProperties(orderInRequest, appPmsOrderIn);
        appPmsOrderIn.setOrderType(Long.valueOf(orderInRequest.getOrderType()));
        appPmsOrderIn.setGoodsNum(BigDecimal.valueOf(orderInRequest.getGoodsNum()));
        appPmsOrderIn.setUsedNum(BigDecimal.valueOf(0));
        appPmsOrderIn.setRemainingNum(BigDecimal.valueOf(orderInRequest.getGoodsNum()));
        appPmsOrderIn.setOrderStatus(Long.valueOf(0));
        appPmsOrderIn.setCreateTime(new Date());
        appPmsOrderIn.setUpdateTime(new Date());


        int i1 = appPmsOrderInService.insertAppPmsOrderIn(appPmsOrderIn);
        if (i1 == 0) {
            return error("入库单保存失败");
        }
        return AjaxResult.success("success");
    }

    /**
     * Pms出库单请求
     */
    @ApiOperation("Pms出库单请求")
    @Log(title = "Pms出库单请求", skipAuth = true)
    @PostMapping("/orderOut")
    @Anonymous
    public AjaxResult pmsOrderOut(@RequestBody List<PmsOrderOutRequest> orderOutRequests) {
        if(orderOutRequests == null || orderOutRequests.isEmpty()) {
            return error("请求数据为空。");
        }
        logger.info("Pms出库单请求：{}", orderOutRequests);
        int insertRow = 0;
        // 判断请求数据完整性
        //生成wms统一订单号
        String orderId= OrderCodeFactory.getOrderCode("WMS", "");
        for(PmsOrderOutRequest orderOutRequest : orderOutRequests) {
            if (StringUtils.isEmpty(orderOutRequest.getListId())
                    || orderOutRequest.getOrderType() == null
                    || StringUtils.isEmpty(orderOutRequest.getGoodsId())
                    || orderOutRequest.getGoodsNum() == null
                    || StringUtils.isEmpty(orderOutRequest.getGoodsDesc())) {
                return error("请求数据不完整。");
            }
            // 判断入库单号是否重复
            AppPmsOrderOut existAppPmsOrderOut = appPmsOrderOutService.selectAppPmsOrderOutByListId(orderOutRequest.getListId());
            if (existAppPmsOrderOut != null) {
                return error("出库单号重复。");
            }
            AppPmsOrderOut appPmsOrderOut = new AppPmsOrderOut();
            appPmsOrderOut.setRecordId(UUID.randomUUID().toString());
            appPmsOrderOut.setOrderId(orderId);
            appPmsOrderOut.setListId(orderOutRequest.getListId());
            appPmsOrderOut.setOrderType(Long.valueOf(orderOutRequest.getOrderType()));
            appPmsOrderOut.setCustomerId(orderOutRequest.getCustomerId());
            appPmsOrderOut.setGoodsId(orderOutRequest.getGoodsId());
            appPmsOrderOut.setGoodsNum(BigDecimal.valueOf(orderOutRequest.getGoodsNum()));
            appPmsOrderOut.setGoodsDesc(orderOutRequest.getGoodsDesc());
            appPmsOrderOut.setOrderStatus(0);
            appPmsOrderOut.setCreateTime(new Date());
            appPmsOrderOut.setUpdateTime(new Date());
            appPmsOrderOut.setCreateBy("PMS出库接口");
            insertRow += appPmsOrderOutService.insertAppPmsOrderOut(appPmsOrderOut);
        }

        return insertRow == orderOutRequests.size() ? success("success") : error("出库单保存失败");
    }

    /**
     * 模糊查询，查询入库单表
     * @param param 查询参数
     * @return 返回结果
     */
    @Anonymous
    @GetMapping(value = "/getInfoWithString")
    public AjaxResult getInfoWithString(@RequestParam("param") String param)
    {
        return success(appPmsOrderInService.getInfoWithString(param));
    }

    @Anonymous
    @PostMapping("/pmsStockInComplete")
    public AjaxResult pmsStockInComplete(@RequestBody PmsStockInCompleteReq request) {
        return toAjax(appPmsOrderInService.pmsStockInComplete(request));
    }

}
