package com.fs.his.service.impl;

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

import com.fs.common.core.domain.AjaxResult;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.mapper.FsStoreOrderMapper;
import com.fs.his.mapper.FsStorePaymentMapper;
import com.fs.his.param.FsStoreOrderParam;
import com.fs.his.param.FsStorePaymentParam;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.vo.FsStoreOrderExcelVO;
import com.fs.his.vo.FsStoreOrderExportVO;
import com.fs.his.vo.FsStorePaymentExcelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsExportTaskMapper;
import com.fs.his.domain.FsExportTask;
import com.fs.his.service.IFsExportTaskService;

/**
 * 导出任务Service业务层处理
 *
 * @author fs
 * @date 2024-11-19
 */
@Service
public class FsExportTaskServiceImpl implements IFsExportTaskService
{
    @Autowired
    private FsExportTaskMapper fsExportTaskMapper;
    @Autowired
    FsStoreOrderMapper fsStoreOrderMapper;
    @Autowired

    FsStorePaymentMapper fsStorePaymentMapper;
    /**
     * 查询导出任务
     *
     * @param taskId 导出任务主键
     * @return 导出任务
     */
    @Override
    public FsExportTask selectFsExportTaskByTaskId(Long taskId)
    {
        return fsExportTaskMapper.selectFsExportTaskByTaskId(taskId);
    }

    /**
     * 查询导出任务列表
     *
     * @param fsExportTask 导出任务
     * @return 导出任务
     */
    @Override
    public List<FsExportTask> selectFsExportTaskList(FsExportTask fsExportTask)
    {
        return fsExportTaskMapper.selectFsExportTaskList(fsExportTask);
    }

    /**
     * 新增导出任务
     *
     * @param fsExportTask 导出任务
     * @return 结果
     */
    @Override
    public int insertFsExportTask(FsExportTask fsExportTask)
    {
        return fsExportTaskMapper.insertFsExportTask(fsExportTask);
    }

    /**
     * 修改导出任务
     *
     * @param fsExportTask 导出任务
     * @return 结果
     */
    @Override
    public int updateFsExportTask(FsExportTask fsExportTask)
    {
        return fsExportTaskMapper.updateFsExportTask(fsExportTask);
    }

    /**
     * 批量删除导出任务
     *
     * @param taskIds 需要删除的导出任务主键
     * @return 结果
     */
    @Override
    public int deleteFsExportTaskByTaskIds(Long[] taskIds)
    {
        return fsExportTaskMapper.deleteFsExportTaskByTaskIds(taskIds);
    }

    /**
     * 删除导出任务信息
     *
     * @param taskId 导出任务主键
     * @return 结果
     */
    @Override
    public int deleteFsExportTaskByTaskId(Long taskId)
    {
        return fsExportTaskMapper.deleteFsExportTaskByTaskId(taskId);
    }

    @Override
    public Integer isExportType1(Long userId) {

        return fsExportTaskMapper.isExportType1(userId);
    }

    @Async
    @Override
    public void exportStore1Data(FsStoreOrderParam fsStoreOrder) {
        List<FsStoreOrderExportVO> list = fsStoreOrderMapper.selectFsStoreOrderListVOByExport(fsStoreOrder);
        //对手机号脱敏
        if (list != null) {
            for (FsStoreOrderExportVO vo : list) {
                vo.setFollowFrequency(6);
                if (vo.getCycle()!=null){
                    vo.setFollowCount(vo.getCycle()/ vo.getFollowFrequency());
                }
                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<FsStoreOrderExportVO> util = new ExcelUtil<FsStoreOrderExportVO>(FsStoreOrderExportVO.class);
        AjaxResult result= util.exportExcel(list, "订单数据");
        FsExportTask task=fsExportTaskMapper.selectFsExportTaskByTaskId(fsStoreOrder.getTaskId());
        task.setFinishTime(new Date());
        task.setStatus(1);
        task.setFileUrl(result.get("msg").toString());
        fsExportTaskMapper.updateFsExportTask(task);
    }
    @Async
    @Override
    public void exportStoreData(FsStoreOrderParam fsStoreOrder) {
        List<FsStoreOrderExcelVO> list = fsStoreOrderMapper.selectFsStoreOrderExcelListVO(fsStoreOrder);
        ExcelUtil<FsStoreOrderExcelVO> util = new ExcelUtil<FsStoreOrderExcelVO>(FsStoreOrderExcelVO.class);
        AjaxResult result= util.exportExcel(list, "药品订单财务导出");
        FsExportTask task=fsExportTaskMapper.selectFsExportTaskByTaskId(fsStoreOrder.getTaskId());
        task.setFinishTime(new Date());
        task.setStatus(1);
        task.setFileUrl(result.get("msg").toString());
        fsExportTaskMapper.updateFsExportTask(task);
    }
    @Async
    @Override
    public void exportStorePaymentData(FsStorePaymentParam fsStorePayment) {

            List<FsStorePaymentExcelVO> list = fsStorePaymentMapper.selectFsStorePaymentExcelVO(fsStorePayment);
            ExcelUtil<FsStorePaymentExcelVO> util = new ExcelUtil<FsStorePaymentExcelVO>(FsStorePaymentExcelVO.class);
            AjaxResult result =  util.exportExcel(list, "支付明细数据");
            FsExportTask task=fsExportTaskMapper.selectFsExportTaskByTaskId(fsStorePayment.getTaskId());
            task.setFinishTime(new Date());
            task.setStatus(1);
            task.setFileUrl(result.get("msg").toString());
            fsExportTaskMapper.updateFsExportTask(task);

    }

    @Override
    public Integer isExportType2(Long userId) {
         return fsExportTaskMapper.isExportType2(userId);
    }
}
