package com.jwipc.shopping.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.dto.ExportDataDTO;
import com.jwipc.exception.DaoException;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.shopping.constant.ModuleConst;
import com.jwipc.shopping.dto.ShoppingPayDTO;
import com.jwipc.shopping.logic.ShoppingPayLogic;
import com.jwipc.shopping.model.po.ShoppingPayDetailPO;
import com.jwipc.shopping.model.query.CostCenterQuery;
import com.jwipc.shopping.model.query.ShoppingPayDetailQuery;
import com.jwipc.shopping.model.query.ShoppingPayQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
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.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: swq
 * @Date: 2024/4/10 12:43
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/shoppingPay/rest")
@RequiredArgsConstructor
@Slf4j
public class ShoppingPayRestController {
    @Autowired
    ShoppingPayLogic shoppingPayLogic;

    @AuditLogInter(value = "列表查询", module = "商城付款")
    @RequestMapping("/list")
    public PageView listPage(ShoppingPayQuery query) {
        return shoppingPayLogic.shoppingPayListPage(query);
    }

    @PostMapping("/findPayDetail")
    public ResponseEntity findPayDetail(ShoppingPayDetailQuery query){
        return ResponseEntity.ok(shoppingPayLogic.findPayDetail(query));
    }

    @GetMapping("/costCenterList")
    public PageView costCenterList(CostCenterQuery query) {
        return shoppingPayLogic.costCenterList(query);
    }

    @GetMapping("/costCenterListAll")
    public ResponseEntity costCenterListAll(CostCenterQuery query) {
        return ResponseEntity.ok(shoppingPayLogic.costCenterListAll(query));
    }

    @RequestMapping("/form/formPayData")
    @Transactional
    public ResponseEntity formPayData(ShoppingPayDTO bean, String orderlist ) {
        //检查编码是否存在
        Long count = shoppingPayLogic.selectOnlyPayCode(bean.getPayCode(), bean.getId());
        if (count > 0) {
            throw new DaoException("编码已存在，请修改!");
        }
        if (!StringUtils.isEmpty(bean.getId())) {
            bean.setUpdateTime(LocalDateTime.now());
            bean.setUpdateUser(LoginUtil.getUserId());
        }
        shoppingPayLogic.saveShoppingPayDeatils(bean,orderlist);
        return ResponseEntity.ok(true);
    }
    @AuditLogInter(value = "查看单条数据", module = "商城付款")
    @RequestMapping("/detailOrderLists")
    public PageView detailOrderLists(ShoppingPayDetailQuery query) {
        return shoppingPayLogic.detailOrderLists(query);
    }

    @RequestMapping("/detailRemove")
    @Transactional
    public ResponseEntity detailRemove(String id) {
        shoppingPayLogic.removeShoppingPayDetail(id);
        return ResponseEntity.ok(true);
    }

    @RequestMapping("/payRemove")
    @Transactional
    public ResponseEntity payRemove(String id) {
        shoppingPayLogic.removeShoppingPay(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 明细导出
     * @param queryStr 查询条件
     */
    @PostMapping("/detail/export")
    public void priceExport(HttpServletResponse response, String queryStr) throws IOException {
        List<ExportDataDTO> exportDataDTOS = new ArrayList<>();
        //先加载固定字段
//        exportDataDTOS.add(ExportDataDTO.builder().title("").field("factoryCode").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("物料凭证号").field("mvCode").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("行号").field("mvItemNo").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("其他折扣").field("deductionRatio").color(IndexedColors.YELLOW.getIndex()).build());
        exportDataDTOS.add(ExportDataDTO.builder().title("标题").field("title").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("OA流程单号").field("oaProcessId").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("流程编号").field("payCode").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("订单编码").field("cartCode").build());//主表
        exportDataDTOS.add(ExportDataDTO.builder().title("商品名称").field("skuName").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("单价").field("unitCost").build());//收货表
        exportDataDTOS.add(ExportDataDTO.builder().title("商品数量").field("qty").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("商品数量").field("createTime").exportDateFormatter("dateTime").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("单位").field("unit").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("收货数量").field("receiptNum").build());

//        exportDataDTOS.add(ExportDataDTO.builder().title("省/市/地区").field("provinceName + / + cityName + / + countyName").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("收货金额").field("totalAmount").build());

        exportDataDTOS.add(ExportDataDTO.builder().title("实际收货数量").field("actualReceivedQuantity").build());//收货表
        exportDataDTOS.add(ExportDataDTO.builder().title("实际付款总金额").field("actualTotalAmount").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("报销类别").field("payCategory").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("业务描述").field("serviceDesc").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("使用人").field("createUserName").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("申请单号").field("purCode").build());//info表
        exportDataDTOS.add(ExportDataDTO.builder().title("项目编号").field("projectNo").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("服务客户").field("customerCode").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("服务客户实际值").field("customerName").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("驱动因素").field("drivingFactorsText").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("成本中心").field("costCenterCode").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("成本中心名称").field("costCenterName").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("收货时间").field("receivedTime").exportDateFormatter("dateTime").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("退货数量").field("returnQuatity").build());//售后明细表
//
//
//        exportDataDTOS.add(ExportDataDTO.builder().title("采购申请单号").field("prCode").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("OA流程单号").field("purchaseOrderNumber").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("发货单号").field("packageId").build());
//        exportDataDTOS.add(ExportDataDTO.builder().title("发票号").field("invoiceNumber").build());
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        ShoppingPayDetailQuery query = JSON.parseObject(queryStr, ShoppingPayDetailQuery.class);
        final List<ShoppingPayDetailPO> exportData = shoppingPayLogic.selectPayExportData(query);

        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"购物订单明细-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, JSON.toJSONString(exportDataDTOS));
    }
}
