package cn.iocoder.foodnexus.module.order.controller.admin.customerorder;

import cn.iocoder.foodnexus.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.foodnexus.framework.common.pojo.CommonResult;
import cn.iocoder.foodnexus.framework.common.pojo.PageParam;
import cn.iocoder.foodnexus.framework.common.pojo.PageResult;
import cn.iocoder.foodnexus.framework.common.util.CommonUtil;
import cn.iocoder.foodnexus.framework.common.util.number.MoneyUtils;
import cn.iocoder.foodnexus.framework.common.util.object.BeanUtils;
import cn.iocoder.foodnexus.framework.excel.core.util.ExcelUtils;
import cn.iocoder.foodnexus.module.erp.controller.admin.product.vo.unit.ErpProductUnitPageReqVO;
import cn.iocoder.foodnexus.module.erp.controller.admin.product.vo.unit.ErpProductUnitRespVO;
import cn.iocoder.foodnexus.module.erp.dal.dataobject.product.ErpProductUnitDO;
import cn.iocoder.foodnexus.module.order.controller.admin.customerorder.vo.CustomerOrderReconExportRespVO;
import cn.iocoder.foodnexus.module.order.controller.admin.customerorder.vo.CustomerOrderReconPageReqVO;
import cn.iocoder.foodnexus.module.order.controller.admin.customerorder.vo.CustomerOrderReconPageRespVO;
import cn.iocoder.foodnexus.module.order.service.customerorder.CustomerOrderService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.foodnexus.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.foodnexus.framework.common.pojo.CommonResult.success;

/**
 * @author : yanghao
 * create at:  2025/11/3  16:16
 * @description: 管理后台 - 客户对账
 */
@RestController
@RequestMapping("/oper/customr-order/reconciliation")
@Validated
public class OperCustomerOrderReconciliationController {

    @Autowired
    private CustomerOrderService customerOrderService;

    @GetMapping("/page")
    @Operation(summary = "分页获取月度账单")
    public CommonResult<PageResult<CustomerOrderReconPageRespVO>> page(@Valid CustomerOrderReconPageReqVO pageReqVO) {
        return success(customerOrderService.pageReconciliation(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户对账 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid CustomerOrderReconPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerOrderReconPageRespVO> list = customerOrderService.pageReconciliation(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户对账.xls", "数据", CustomerOrderReconExportRespVO.class,
                BeanUtils.toBean(list, CustomerOrderReconExportRespVO.class, item -> {
                    if (CommonUtil.isNotEmpty(item.getIsFinish())) {
                        item.setIsFinishStr(item.getIsFinish() ? "已对账" : "未对账");
                    } else {
                        item.setIsFinishStr("未对账");
                    }
                    item.setOrderAmountYuan(MoneyUtils.fenToYuan(item.getOrderAmount()));
                    item.setActualAmountYuan(MoneyUtils.fenToYuan(item.getActualAmount()));
                }));
    }

}
