package com.hfkj.service.agent.impl;

import com.hfkj.common.security.UserCenter;
import com.hfkj.dao.BsAgentApiAccountRecordMapper;
import com.hfkj.entity.BsAgentApiAccountRecord;
import com.hfkj.entity.BsAgentApiAccountRecordExample;
import com.hfkj.entity.BsAgentMerAccountRecord;
import com.hfkj.entity.BsAgentMerAccountRecordExample;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.agent.BsAgentApiAccountRecordService;
import com.hfkj.service.agent.BsAgentMerAccountRecordService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsAgentApiAccountRecordServiceImpl
 * @author: HuRui
 * @date: 2024/11/19
 **/
@Service("agentApiAccountRecordService")
public class BsAgentApiAccountRecordServiceImpl implements BsAgentApiAccountRecordService {
    @Resource
    private BsAgentApiAccountRecordMapper agentApiAccountRecordMapper;
    @Resource
    private UserCenter userCenter;

    @Override
    public void create(BsAgentApiAccountRecord data) {
        try {
            SecUserSessionObject secUserSessionObject = userCenter.getSessionModel(SecUserSessionObject.class);
            if (secUserSessionObject != null) {
                data.setOpUserType(1);
                data.setOpUserId(secUserSessionObject.getAccount().getId());
                data.setOpUserName(secUserSessionObject.getAccount().getUserName());
                data.setOpUserPhone(secUserSessionObject.getAccount().getTelephone());
            }
        } catch (Exception e) {
            System.out.println("获取管理员信息失败");
        }

        try {
            UserSessionObject userModel = userCenter.getSessionModel(UserSessionObject.class);
            if (userModel != null) {
                data.setOpUserType(2);
                data.setOpUserId(userModel.getUser().getId());
                data.setOpUserName(userModel.getUser().getUserName());
                data.setOpUserPhone(userModel.getUser().getPhone());
            }
        } catch (Exception e) {
            System.out.println("获取客户信息失败");
        }
        data.setStatus(1);
        data.setCreateTime(new Date());
        agentApiAccountRecordMapper.insert(data);
    }

    @Override
    public List<BsAgentApiAccountRecord> getList(Map<String, Object> param) {
        BsAgentApiAccountRecordExample example = new BsAgentApiAccountRecordExample();
        BsAgentApiAccountRecordExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getLong(param, "agentApiAccountId") != null) {
            criteria.andAgentApiAccountIdEqualTo(MapUtils.getLong(param, "agentApiAccountId"));
        }

        if (MapUtils.getLong(param, "agentId") != null) {
            criteria.andAgentIdEqualTo(MapUtils.getLong(param, "agentId"));
        }

        if (MapUtils.getInteger(param, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(param, "type"));
        }

        if (MapUtils.getInteger(param, "sourceType") != null) {
            criteria.andSourceTypeEqualTo(MapUtils.getInteger(param, "sourceType"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "sourceOrderNo"))) {
            criteria.andSourceOrderNoEqualTo(MapUtils.getString(param, "sourceOrderNo"));
        }

        if (MapUtils.getLong(param, "createTimeS") != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeS")));
        }

        if (MapUtils.getLong(param, "createTimeE") != null) {
            criteria.andCreateTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeE")));
        }

        example.setOrderByClause("create_time desc");
        return agentApiAccountRecordMapper.selectByExample(example);
    }
}
