package com.jwipc.shopping.controller.pc;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.dto.ExportDataDTO;
import com.jwipc.security.LoginUtil;
import com.jwipc.shopping.constant.ModuleConst;
import com.jwipc.shopping.dto.OnlineMallResponseDTO;
import com.jwipc.shopping.logic.OnlineMallLogic;
import com.jwipc.shopping.model.query.OnlineMallCommodityQuery;
import com.jwipc.shopping.model.vo.*;
import com.jwipc.sys.client.DictClient;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/13 0013 16:28
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/shopping/rest")
@RequiredArgsConstructor
@Slf4j
public class ShoppingMallRestController {

    @Autowired
    OnlineMallLogic onlineMallLogic;

    @Autowired
    DictClient dictClient;


    /**
     * 获取商城商品信息
     *
     * @param query 查询条件
     * @return 结果
     */
    @RequestMapping("/find/commodity")
    public ResponseEntity<?> findOnlineMallCommodity(OnlineMallCommodityQuery query){
        if(StringUtils.isEmpty(query.getSources())){
            JSONObject error = new JSONObject();
            error.put("status", 500);
            error.put("message", "参数错误, 请选择商城");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
        }
        query.setUser_code(LoginUtil.getAccount());
        OnlineShoppingZkhResponse result = onlineMallLogic.findOnlineMallCommodity(query);
        if(!"1".equals(result.getCode())){
            JSONObject error = new JSONObject();
            error.put("status", 500);
            error.put("message", result.getResulteMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
        }
        return ResponseEntity.ok(result);
    }


    /**
     * 登录免密接口获取url
     *
     * @param productDescUrl 产品地址
     * @return 结果
     */
    @RequestMapping("/get/url")
    public ResponseEntity getUrlByZkh(String productDescUrl) {
        if (StringUtils.isEmpty(productDescUrl)) {
            return ResponseEntity.badRequest().body("参数错误, 请联系管理员 ");
        }
        return ResponseEntity.ok(onlineMallLogic.getProductUrlByZkh(productDescUrl));
    }



    /**
     * 选购数据推送商城购物车
     *
     * @param XYParams 选购数据
     * @return 结果
     */
    @RequestMapping("/push/mall/shopping/cart")
    public ResponseEntity<?> pushMallShoppingCart(String XYParams,String ZKHParams, String receivingItem, String invoiceItem, String dateItem){
        if(StringUtils.isEmpty(XYParams) || StringUtils.isEmpty(receivingItem) || StringUtils.isEmpty(invoiceItem) || StringUtils.isEmpty(dateItem)){
            return ResponseEntity.badRequest().body("请求参数异常, 请联系管理员");
        }
        OnlineMallResponseDTO response = onlineMallLogic.pushMallShoppingCart(LoginUtil.getCurrentUser() ,XYParams,ZKHParams, receivingItem, invoiceItem, dateItem);
        return ResponseEntity.ok(response);
    }


    /**
     * 选购数据推送商城购物车
     *
     * @return 结果
     */
    @RequestMapping("/push/mall/excel/cart")
    public ResponseEntity<?> pushMallExcelCart(String params, String receivingItem, String invoiceItem, String dateItem){
        if(StringUtils.isEmpty(params)  || StringUtils.isEmpty(invoiceItem) || StringUtils.isEmpty(dateItem)){
            return ResponseEntity.badRequest().body("请求参数异常, 请联系管理员");
        }
        onlineMallLogic.pushMallExcelCart(LoginUtil.getCurrentUser() ,params, receivingItem, invoiceItem, dateItem);
        return ResponseEntity.ok(true);
    }



    /**
     * 手工推送订单至商城购物车及后续
     *
     * @return 结果
     */
    @PostMapping("/push/cart")
    public ResponseEntity<?> pushPoToCart(Long id) {
        OnlineMallResponse response = onlineMallLogic.pushPoToCart(id);
        return ResponseEntity.ok(response);
    }

    /**
     * 采购订单明细导出
     *
     * @return
     */
    @PostMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response, String queryStr) throws IOException {
        List<ExportDataDTO> exportDataDTOS = new ArrayList<>();
        //先加载固定字段
        exportDataDTOS.add(ExportDataDTO.builder().title("商城编码").field("source").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("来源").field("sourceName").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("商品编码").field("productCode").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("商品名称").field("productName").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("单位").field("productUnit").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("商品价格").field("productPrice").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("数量").field("qty").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("采购申请单号").field("prCode").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("行号").field("prItemNo").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("请购数量").field("requireNum").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("物料描述").field("materialName").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("规格").field("materialSpec").build());
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<ExcelShopCartVO> exportData = JSONArray.parseArray(queryStr, ExcelShopCartVO.class);
        EasyExcelUtils.trendsModelWrite(response,"选购商品数据-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, JSON.toJSONString(exportDataDTOS));

    }


}
