package com.cool.store.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.HyPartnerTaskInfoLogDAO;
import com.cool.store.dto.log.LineLogInfo;
import com.cool.store.dto.log.LogBasicDTO;
import com.cool.store.entity.HyPartnerLineInfoDO;
import com.cool.store.enums.OperateTypeEnum;
import com.cool.store.enums.WorkflowStageEnum;
import com.cool.store.enums.WorkflowStatusEnum;
import com.cool.store.mapper.HyPartnerLineInfoMapper;
import com.cool.store.service.LogService;
import com.cool.store.utils.CoolDateUtils;
import com.cool.store.utils.Md5Utils;
import com.cool.store.vo.PartnerUserInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

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

/**
 * @Author: young.yu
 * @Date: 2023-06-28  00:10
 * @Description:
 */
@Service
public class LogServiceImpl implements LogService {
    private static final Logger LOG = LoggerFactory.getLogger(Md5Utils.class);

    @Autowired
    private HyPartnerLineInfoMapper partnerLineInfoMapper;
    @Autowired
    private HyPartnerTaskInfoLogDAO hyPartnerTaskInfoLogDAO;

    @Override
    @Async
    public  void recordBizLog(LoginUserInfo operator,Long lineId,OperateTypeEnum operateTypeEnum,Object logData){
        try {
            List<HyPartnerLineInfoDO> lineInfos = partnerLineInfoMapper.getHyPartnerLineInfoListByIds(Arrays.asList(lineId));
            if (lineInfos == null || lineInfos.size() == 0) {
                return;
            }
            HyPartnerLineInfoDO hyPartnerLineInfoDO = lineInfos.get(0);
            LineLogInfo lineLogInfo = null;
            if(operator!= null){
                lineLogInfo = new LineLogInfo(hyPartnerLineInfoDO.getPartnerId(), lineId, operator.getUserId(),
                        operator.getName(), operateTypeEnum,
                        WorkflowStageEnum.getWorkflowStageByCode(hyPartnerLineInfoDO.getWorkflowStage()),
                        hyPartnerLineInfoDO.getWorkflowStatus(), "");
            }else {
                lineLogInfo = new LineLogInfo(hyPartnerLineInfoDO.getPartnerId(), lineId, null,
                        null, operateTypeEnum,
                        WorkflowStageEnum.getWorkflowStageByCode(hyPartnerLineInfoDO.getWorkflowStage()),
                        hyPartnerLineInfoDO.getWorkflowStatus(), "");
            }
            lineLogInfo.setData(logData);
            hyPartnerTaskInfoLogDAO.addOperateLog(lineLogInfo);
        }catch (Exception e){
            LOG.error("recordBizLog error",e);
            e.printStackTrace();
        }
    }

    @Override
    @Async
    public  void recordPartnerBizLog(PartnerUserInfoVO operator, Long lineId, OperateTypeEnum operateTypeEnum, Object logData){
        try {
            List<HyPartnerLineInfoDO> lineInfos = partnerLineInfoMapper.getHyPartnerLineInfoListByIds(Arrays.asList(lineId));
            if (lineInfos == null || lineInfos.size() == 0) {
                return;
            }
            HyPartnerLineInfoDO hyPartnerLineInfoDO = lineInfos.get(0);
            LineLogInfo lineLogInfo = new LineLogInfo(hyPartnerLineInfoDO.getPartnerId(), lineId, operator.getPartnerId(),
                    operator.getUsername(), operateTypeEnum,
                    WorkflowStageEnum.getWorkflowStageByCode(hyPartnerLineInfoDO.getWorkflowStage()),
                    hyPartnerLineInfoDO.getWorkflowStatus(), "");
            lineLogInfo.setData(logData);
            hyPartnerTaskInfoLogDAO.addOperateLog(lineLogInfo);
        }catch (Exception e){
            LOG.error("recordBizLog error",e);
            e.printStackTrace();
        }
    }
}
