package com.fs.company.service.impl;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.AjaxResult;
import com.fs.common.utils.DateUtils;
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.CompanyDept;
import com.fs.company.domain.CompanyUser;
import com.fs.company.param.CompanyMoneyLogsParam;
import com.fs.company.param.CompanyStoreOrderMoneyLogsListParam;
import com.fs.company.service.ICompanyDeptService;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.vo.*;
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.mapper.FsExportTaskMapper;
import com.fs.his.param.FsCompanyMoneyLogsExportParam;
import com.fs.his.param.FsStoreOrderParam;
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.FsPrescribeExcelListVO;
import com.fs.his.vo.FsStoreOrderExcelVO;
import com.fs.his.vo.FsStoreOrderExportDataVO;
import com.fs.his.vo.FsStoreOrderExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.fs.company.mapper.CompanyMoneyLogsMapper;
import com.fs.company.domain.CompanyMoneyLogs;
import com.fs.company.service.ICompanyMoneyLogsService;

/**
 * 企业账户记录Service业务层处理
 *
 * @author fs
 * @date 2021-10-04
 */
@Service
public class CompanyMoneyLogsServiceImpl implements ICompanyMoneyLogsService
{
    @Autowired
    private CompanyMoneyLogsMapper companyMoneyLogsMapper;

    @Autowired
    FsExportTaskMapper fsExportTaskMapper;
    /**
     * 查询企业账户记录
     *
     * @param logsId 企业账户记录ID
     * @return 企业账户记录
     */
    @Override
    public CompanyMoneyLogs selectCompanyMoneyLogsById(Long logsId)
    {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsById(logsId);
    }

    /**
     * 查询企业账户记录列表
     *
     * @param companyMoneyLogs 企业账户记录
     * @return 企业账户记录
     */
    @Override
    public List<CompanyMoneyLogs> selectCompanyMoneyLogsList(CompanyMoneyLogs companyMoneyLogs)
    {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsList(companyMoneyLogs);
    }

    /**
     * 新增企业账户记录
     *
     * @param companyMoneyLogs 企业账户记录
     * @return 结果
     */
    @Override
    public int insertCompanyMoneyLogs(CompanyMoneyLogs companyMoneyLogs)
    {
        companyMoneyLogs.setCreateTime(DateUtils.getNowDate());
        return companyMoneyLogsMapper.insertCompanyMoneyLogs(companyMoneyLogs);
    }

    /**
     * 修改企业账户记录
     *
     * @param companyMoneyLogs 企业账户记录
     * @return 结果
     */
    @Override
    public int updateCompanyMoneyLogs(CompanyMoneyLogs companyMoneyLogs)
    {
        return companyMoneyLogsMapper.updateCompanyMoneyLogs(companyMoneyLogs);
    }

    /**
     * 批量删除企业账户记录
     *
     * @param logsIds 需要删除的企业账户记录ID
     * @return 结果
     */
    @Override
    public int deleteCompanyMoneyLogsByIds(Long[] logsIds)
    {
        return companyMoneyLogsMapper.deleteCompanyMoneyLogsByIds(logsIds);
    }

    /**
     * 删除企业账户记录信息
     *
     * @param logsId 企业账户记录ID
     * @return 结果
     */
    @Override
    public int deleteCompanyMoneyLogsById(Long logsId)
    {
        return companyMoneyLogsMapper.deleteCompanyMoneyLogsById(logsId);
    }

    @Override
    public List<CompanyMoneyLogsVO> selectCompanyMoneyLogsVOList(CompanyMoneyLogsParam companyMoneyLogs) {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsVOList(companyMoneyLogs);
    }

    @Override
    public List<CompanyMoneyLogsVO> selectCompanyMoneyLogsMallVOList(CompanyMoneyLogsParam companyMoneyLogs) {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsMallVOList(companyMoneyLogs);
    }

    @Override
    public List<CompanyMoneyLogsVO> selectCompanyMoneyLogsMallVO1List(CompanyMoneyLogsParam companyMoneyLogs) {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsMallVO1List(companyMoneyLogs);
    }

    @Override
    public BigDecimal selectCompanyTotalMoneys(CompanyMoneyLogsParam companyMoneyLogs) {
        return companyMoneyLogsMapper.selectCompanyTotalMoneys(companyMoneyLogs);
    }

    @Override
    public List<CompanyMoneyLogsExportVO> selectCompanyMoneyLogsExportVOList( FsCompanyMoneyLogsExportParam companyMoneyLogs) {

        return companyMoneyLogsMapper.selectCompanyMoneyLogsExportVOList(companyMoneyLogs);
    }

    @Override
    public List<CompanyMoneyLogsExport1VO> selectCompanyMoneyLogsExport1VOList( FsCompanyMoneyLogsExportParam companyMoneyLogs) {

        return companyMoneyLogsMapper.selectCompanyMoneyLogsExport1VOList(companyMoneyLogs);
    }

    @Override
    public List<CompanyMoneyLogsExport2VO> selectCompanyMoneyLogsExport2VOList(CompanyMoneyLogs companyMoneyLogs) {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsExport2VOList(companyMoneyLogs);
    }

    @Override
    public List<CompanyMoneyLogs> selectCompanyMoneyLogsByCompanyId(Long companyId, String minId) {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsByCompanyId(companyId,minId);
    }

    @Override
    public List<CompanyStoreOrderMoneyLogsVO> selectCompanyStoreOrderMoneyLogsList(CompanyStoreOrderMoneyLogsListParam moneyLogsMap) {
        return companyMoneyLogsMapper.selectCompanyStoreOrderMoneyLogsList(moneyLogsMap);
    }

    @Override
    public Boolean isEntityNull(CompanyMoneyLogsParam param) {

        if (param == null) {
            return true; // 整个实体对象为null
        }

        Class<? extends CompanyMoneyLogsParam> entityClass = param.getClass();
        Field[] fields = entityClass.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(param);
                if (value != null) {
                    return false; // 实体对象中有字段不为null
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return true; // 实体对象内所有字段为null
    }

    @Override
    public Integer selectCompanyMoneyLogsExportCounts(FsCompanyMoneyLogsExportParam companyMoneyLogs) {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsExportCounts(companyMoneyLogs);
    }

    @Override
    public Integer selectCompanyMoneyLogsExport1Counts(FsCompanyMoneyLogsExportParam param) {
        return companyMoneyLogsMapper.selectCompanyMoneyLogsExport1Counts(param);
    }

    @Override
    @Async
    public void exportData(FsCompanyMoneyLogsExportParam param) {

        if(param.getType()==0){
            List<CompanyMoneyLogsExportVO> list = companyMoneyLogsMapper.selectCompanyMoneyLogsExportVOList(param);
            ExcelUtil<CompanyMoneyLogsExportVO> util = new ExcelUtil<CompanyMoneyLogsExportVO>(CompanyMoneyLogsExportVO.class);
            AjaxResult result =  util.exportExcel(list, "全部流水");
            FsExportTask task=fsExportTaskMapper.selectFsExportTaskByTaskId(param.getTaskId());
            task.setFinishTime(new Date());
            task.setStatus(1);
            task.setFileUrl(result.get("msg").toString());
            fsExportTaskMapper.updateFsExportTask(task);
        }
        else if(param.getType()==1){
            List<CompanyMoneyLogsExport1VO> list = companyMoneyLogsMapper.selectCompanyMoneyLogsExport1VOList(param);
            if (list != null) {
                for (CompanyMoneyLogsExport1VO vo : list) {
                    if (vo.getUserPhone() != null) {
                        vo.setUserPhone(vo.getUserPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                    }
                    if (vo.getUserAddress() != null) {
                        vo.setUserAddress(ParseUtils.parseAddress(vo.getUserAddress()));
                    }
                }
            }
            ExcelUtil<CompanyMoneyLogsExport1VO> util = new ExcelUtil<CompanyMoneyLogsExport1VO>(CompanyMoneyLogsExport1VO.class);
            AjaxResult result =  util.exportExcel(list, "商城订单");
            FsExportTask task=fsExportTaskMapper.selectFsExportTaskByTaskId(param.getTaskId());
            task.setFinishTime(new Date());
            task.setStatus(1);
            task.setFileUrl(result.get("msg").toString());
            fsExportTaskMapper.updateFsExportTask(task);
        }
    }
}
