package com.cf.imes.module.system.controller.admin.funds.purchase;

import cn.hutool.core.bean.BeanUtil;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.ManagePurchaseRecordRespVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.PurchaseRecordPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.PurchaseRecordRespVO;
import com.cf.imes.module.system.service.funds.purchase.PurchaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
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 com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

/**
 * @author 软件购买
 */
@Tag(name = "管理后台 - 软件购买")
@RestController
@RequestMapping("/system/purchase")
@Validated
public class PurchaseController {

    @Resource
    private PurchaseService purchaseService;


    @GetMapping("")
    @Operation(summary = "获取软件的购买记录")
    public CommonResult<PageResult<PurchaseRecordRespVO>> getPurchaseRecord(@Valid PurchaseRecordPageReqVO pageReqVO) {
        // 非管理端只能查看本组织数据
        boolean manageEndPoint = SecurityFrameworkUtils.isManageEndPoint();
        if (!manageEndPoint) {
            pageReqVO.setOrganId(SecurityFrameworkUtils.getUserOrganId());
        }
        return success(purchaseService.getPurchaseRecord(pageReqVO));

    }

    @GetMapping("/export")
    @Operation(summary = "导出软件购买记录")
    @OperateLog(type = EXPORT)
    public void billDetailsExport(@Valid PurchaseRecordPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        // 非管理端只能查看本组织数据
        boolean manageEndPoint = SecurityFrameworkUtils.isManageEndPoint();
        if (!manageEndPoint) {
            pageReqVO.setOrganId(SecurityFrameworkUtils.getUserOrganId());
        }
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PurchaseRecordRespVO> list = purchaseService.getPurchaseRecord(pageReqVO).getList();
        if (SecurityFrameworkUtils.isManageEndPoint()) {
            // 导出 Excel
            List<ManagePurchaseRecordRespVO> managePurchaseRecordRespVOS = BeanUtil.copyToList(list, ManagePurchaseRecordRespVO.class);
            ExcelUtils.write(response, "软件购买记录表.xls", "数据", ManagePurchaseRecordRespVO.class, managePurchaseRecordRespVOS);
        } else {
            // 导出 Excel
            ExcelUtils.write(response, "软件购买记录表.xls", "数据", PurchaseRecordRespVO.class, list);
        }
    }
}
