package com.fs.company.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.Company;
import com.fs.company.domain.CompanyDept;
import com.fs.company.domain.CompanyMoneyLogs;
import com.fs.company.domain.CompanyUser;
import com.fs.company.param.CompanyMoneyLogsParam;
import com.fs.company.service.ICompanyDeptService;
import com.fs.company.service.ICompanyMoneyLogsService;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.vo.CompanyMoneyLogsExport1VO;
import com.fs.company.vo.CompanyMoneyLogsExport2VO;
import com.fs.company.vo.CompanyMoneyLogsExportVO;
import com.fs.company.vo.CompanyMoneyLogsVO;
import com.fs.his.domain.FsExportTask;
import com.fs.his.domain.FsInquiryOrder;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.domain.FsStorePayment;
import com.fs.his.param.FsCompanyMoneyLogsExportParam;
import com.fs.his.service.IFsExportTaskService;
import com.fs.his.service.IFsInquiryOrderService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.service.IFsStorePaymentService;
import com.fs.his.vo.FsStoreOrderExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 企业账户记录Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/companyMoneyLogs")
public class CompanyMoneyLogsController extends BaseController
{
    @Autowired
    private ICompanyMoneyLogsService companyMoneyLogsService;

    @Autowired
    private IFsExportTaskService exportTaskService;

    /**
     * 查询企业账户记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyMoneyLogs:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyMoneyLogsParam companyMoneyLogs)
    {
        startPage();
        if(!StringUtils.isEmpty(companyMoneyLogs.getCreateTimeRange())){
            companyMoneyLogs.setCreateTimeList(companyMoneyLogs.getCreateTimeRange().split("--"));
        }
        if(companyMoneyLogs.getLogsType()!=null){
            if(companyMoneyLogs.getLogsType()==3 || companyMoneyLogs.getLogsType()==4 || companyMoneyLogs.getLogsType()==5 || companyMoneyLogs.getLogsType()==6 || companyMoneyLogs.getLogsType()==13|| companyMoneyLogs.getLogsType()==14){
                // 根据type字段判断，支持商城订单(type=0)和直播订单(type=1)
                List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsMallVOList(companyMoneyLogs);
                return getDataTable(list);
            }
        }
        List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsVOList(companyMoneyLogs);
        return getDataTable(list);
    }


    //转账记录
    @PreAuthorize("@ss.hasPermi('company:companyMoneyLogs:list1')")
    @GetMapping("/list1")

    public TableDataInfo list1(CompanyMoneyLogsParam companyMoneyLogs)
    {
        startPage();
        companyMoneyLogs.setLogsType(1);
        List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsVOList(companyMoneyLogs);
        return getDataTable(list);
    }
    //转账记录
    @PreAuthorize("@ss.hasPermi('company:companyMoneyLogs:list2')")
    @GetMapping("/list2")

    public TableDataInfo list2(CompanyMoneyLogsParam companyMoneyLogs) {
        startPage();
        companyMoneyLogs.setLogsType(2);
        List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsVOList(companyMoneyLogs);
        return getDataTable(list);
    }
    //扣款
    @PreAuthorize("@ss.hasPermi('company:companyMoneyLogs:list3')")
    @GetMapping("/list3")
    public TableDataInfo list3(CompanyMoneyLogsParam companyMoneyLogs) {
        startPage();
        companyMoneyLogs.setLogsType(3);
        List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsVOList(companyMoneyLogs);
        return getDataTable(list);
    }
    /**
     * 导出企业账户记录列表
     */
    @PreAuthorize("@ss.hasAnyPermi('company:companyMoneyLogs:export,company:companyMoneyLogs:export1,company:companyMoneyLogs:export2')")
    @Log(title = "企业账户记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCompanyMoneyLogsExportParam param)   {
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        Integer exportType1 = exportTaskService.isExportType1(SecurityUtils.getUserId());
        if (exportType1>0){
            return AjaxResult.error("你已经有正在导出的任务");
        }
        //导出全部
        if(param.getType().equals(0)){
            //判断是否达到数据上线
            Integer count=companyMoneyLogsService.selectCompanyMoneyLogsExportCounts(param);
            if(count>30000){
                return AjaxResult.error("导出数据已超出上限，最多可导出30000条数据");
            }
            FsExportTask task=new FsExportTask();
            task.setTaskType(1);
            task.setStatus(0);
            task.setStartTime(new Date());
            task.setRemark("账户流水导出");
            task.setSysType(1);
            task.setUserId(SecurityUtils.getUserId());
            exportTaskService.insertFsExportTask(task);
            param.setTaskId(task.getTaskId());
            companyMoneyLogsService.exportData(param);
            return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());
        }
        else if(param.getType().equals(1))
        {
            Integer count=companyMoneyLogsService.selectCompanyMoneyLogsExport1Counts(param);
            if(count>30000){
                return AjaxResult.error("导出数据已超出上限，最多可导出30000条数据");
            }
            FsExportTask task=new FsExportTask();
            task.setTaskType(1);
            task.setStatus(0);
            task.setStartTime(new Date());
            task.setSysType(1);
            String remark = "导出订单明细";
            if (param.getOrderType() != null) {
                if (param.getOrderType() == 0) {
                    remark = "导出商城订单明细";
                } else if (param.getOrderType() == 1) {
                    remark = "导出直播订单明细";
                }
            }
            task.setRemark(remark);
            task.setUserId(SecurityUtils.getUserId());
            exportTaskService.insertFsExportTask(task);
            param.setTaskId(task.getTaskId());
            companyMoneyLogsService.exportData(param);
            return new AjaxResult(200,"后台正在导出,任务ID:"+task.getTaskId(),task.getTaskId());
        }
        return new AjaxResult(500,"导出失败");

    }

    /**
     * 获取企业账户记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyMoneyLogs:query')")
    @GetMapping(value = "/{logsId}")
    public AjaxResult getInfo(@PathVariable("logsId") Long logsId) {
        return AjaxResult.success(companyMoneyLogsService.selectCompanyMoneyLogsById(logsId));
    }

}
