package com.tuoheng.admin.service.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Business;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.BusinessMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.common.core.constant.CommonConstants;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务处理表 服务实现类
 *
 * @author WangHaoran
 * @since 2022-03-11
 */
@Service
@Slf4j
public class BusinessServiceImpl extends BaseServiceImpl<BusinessMapper, Business> implements IBusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Override
    public void insertBusiness(String msgId, String typeId) {

        // 将消息任务和kafka任务id消息存入数据库，用于监听获取任务信息
        Business business = new Business();
        business.setMsgId(msgId);
        business.setType(CommonConstants.INSPECTION_TASK_TYPE);
        business.setTypeId(typeId);
        business.setCreateUser(CurrentUserUtil.getUserId());
        business.setCreateTime(DateUtils.now());
        business.setUpdateUser(CurrentUserUtil.getUserId());
        business.setUpdateTime(DateUtils.now());
        business.setTenantId(CurrentUserUtil.getTenantId());
        business.setMark(MarkEnum.VALID.getCode());
        log.info("thbusiness：{}", JacksonUtil.obj2StringPretty(business));
        // 查询任务是否存在
        int result = businessMapper.selectCount(new LambdaQueryWrapper<Business>()
                .eq(Business::getMsgId, msgId)
                .eq(Business::getTenantId, CurrentUserUtil.getTenantId())
                .eq(Business::getMark, MarkEnum.VALID.getCode()));
        if(result > 0){
            log.info("更新");
            businessMapper.update(business, new LambdaQueryWrapper<Business>()
                    .eq(Business::getMsgId, msgId)
                    .eq(Business::getTenantId, CurrentUserUtil.getTenantId())
                    .eq(Business::getMark, MarkEnum.VALID.getCode()));
        } else {
            log.info("插入");
            businessMapper.insert(business);
        }
    }

    /**
     * 针对于上传时的操作
     * @param msgId
     * @param typeId
     */
    @Override
    public void insertBusinessUploadUrl(String msgId, String typeId) {
        Inspection inspection = inspectionMapper.selectById(typeId);

        // 将消息任务和kafka任务id消息存入数据库，用于监听获取任务信息
        Business business = new Business();
        business.setMsgId(msgId);
        business.setType(CommonConstants.INSPECTION_TASK_TYPE);
        business.setTypeId(typeId);
        business.setCreateUser(inspection.getCreateUser());
        business.setCreateTime(DateUtils.now());
        business.setUpdateUser(inspection.getUpdateUser());
        business.setUpdateTime(DateUtils.now());
        business.setTenantId(inspection.getTenantId());
        business.setMark(MarkEnum.VALID.getCode());
        log.info("thbusiness：{}", JacksonUtil.obj2StringPretty(business));

        // 查询任务是否存在
        int result = businessMapper.selectCount(new LambdaQueryWrapper<Business>()
                .eq(Business::getMsgId, msgId)
                .eq(Business::getTenantId, inspection.getTenantId())
                .eq(Business::getMark, MarkEnum.VALID.getCode()));
        if(result > 0){
            log.info("更新");
            businessMapper.update(business, new LambdaQueryWrapper<Business>()
                    .eq(Business::getMsgId, msgId)
                    .eq(Business::getTenantId, inspection.getTenantId())
                    .eq(Business::getMark, MarkEnum.VALID.getCode()));
        } else {
            log.info("插入");
            businessMapper.insert(business);
        }
    }
}
