package com.fs.company.controller.company;

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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.company.param.CompanyMoneyLogsParam;
import com.fs.company.service.ICompanyMoneyLogsService;
import com.fs.company.vo.CompanyMoneyLogsVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.his.domain.FsExportTask;
import com.fs.his.param.FsCompanyMoneyLogsExportParam;
import com.fs.his.service.IFsExportTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
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 TokenService tokenService;
    @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("--"));
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyMoneyLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        if(companyMoneyLogs.getLogsType()!=null){
            if(companyMoneyLogs.getLogsType()==3 || companyMoneyLogs.getLogsType()==4 || companyMoneyLogs.getLogsType()==5 || companyMoneyLogs.getLogsType()==6){
                List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsMallVOList(companyMoneyLogs);
                return getDataTable(list);
            }
        }
        List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsVOList(companyMoneyLogs);
        return getDataTable(list);
    }

    @GetMapping("/getCompanyTotalMoneys")
    public R getCompanyTotalMoneys(CompanyMoneyLogsParam companyMoneyLogs)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyMoneyLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        BigDecimal totalMoneys = companyMoneyLogsService.selectCompanyTotalMoneys(companyMoneyLogs);
        return R.ok().put("totalMoneys",totalMoneys);
    }



    /**
     * 导出企业账户记录列表
     */
    @PreAuthorize("@ss.hasAnyPermi('company:companyMoneyLogs:export,company:companyMoneyLogs:export1')")
    @Log(title = "企业账户记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCompanyMoneyLogsExportParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        Long userId = loginUser.getUser().getUserId();
        logger.info("tc>\n【导出企业账户记录】：{}",userId );
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }

        Integer exportType1 = exportTaskService.isExportType2(userId);
        if (exportType1>0){
            return AjaxResult.error("你已经有正在导出的任务");
        }

        if(param.getType()==0){
            Integer count=companyMoneyLogsService.selectCompanyMoneyLogsExportCounts(param);
            if (count>30000){
                return AjaxResult.error("导出数据不可超过3w条");
            }
            FsExportTask task=new FsExportTask();
            task.setTaskType(7);
            task.setStatus(0);
            task.setStartTime(new Date());
            task.setRemark("药品订单导出");
            task.setSysType(2);
            task.setCompanyUserId(userId);
            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(7);
            task.setStatus(0);
            task.setStartTime(new Date());
            task.setRemark("药品订单导出");
            task.setSysType(2);
            task.setCompanyUserId(userId);
            exportTaskService.insertFsExportTask(task);
            param.setTaskId(task.getTaskId());
            companyMoneyLogsService.exportData(param);
            return new AjaxResult(200,"后台正在导出,请等待...任务ID:"+task.getTaskId(),task.getTaskId());
        }
        return new AjaxResult(500,"导出失败");

    }


}
