package com.hzs.system.sys.controller.manage;

import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.system.sys.param.BusinessLogParam;
import com.hzs.system.sys.service.ISysBusinessLogService;
import com.hzs.system.sys.vo.BusinessLogVO;
import org.springframework.beans.factory.annotation.Autowired;
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.util.List;
import java.util.Map;

/**
 * @Description: 业务操作日志控制器
 * @Author: jiang chao
 * @Time: 2023/8/2 17:28
 * @Classname: SysBusinessLogController
 * @PackageName: com.hzs.system.sys.controller.manage
 */
@RestController
@RequestMapping("/manage/business-log")
public class SysBusinessLogController extends BaseController {

    @Autowired
    private ISysBusinessLogService iSysBusinessLogService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * 列表
     *
     * @param param 查询入参
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo List(BusinessLogParam param) {
        DateUtils.handTimeStartAndEnd(param.getOperatorTime());
        DateUtils.handTimeStartAndEnd(param.getApproveTime());

        startPage();
        return getDataTable(iSysBusinessLogService.queryList(param, SecurityUtils.getPkCountry()));
    }

    /**
     * 导出
     *
     * @param response
     * @param param    查询入参
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessLogParam param) {
        DateUtils.handTimeStartAndEnd(param.getOperatorTime());
        DateUtils.handTimeStartAndEnd(param.getApproveTime());

        // 最终返回数量
        List<BusinessLogVO> resultList = iSysBusinessLogService.queryList(param, SecurityUtils.getPkCountry());

        if (StringUtils.isNotEmpty(resultList)) {
            // 获取需要翻译的枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EApprovalBusiness.values(), EApproveStatus.values());

            for (BusinessLogVO businessLogVO : resultList) {
                businessLogVO.setBusinessTypeVal(transactionMap.get(EnumsPrefixConstants.APPROVAL_BUSINESS + businessLogVO.getBusinessType()));
                businessLogVO.setBusinessStatusVal(transactionMap.get(EnumsPrefixConstants.APPROVE_STATUS + businessLogVO.getBusinessStatus()));
            }
        }

        ExcelUtil<BusinessLogVO> util = new ExcelUtil<>(BusinessLogVO.class);
        util.exportExcel(response, resultList, "操作明细导出");
    }
}
