package com.hfkj.service.agent.impl;

import com.hfkj.common.security.UserCenter;
import com.hfkj.dao.BsAgentMerAccountRecordMapper;
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.BsAgentMerAccountRecordService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsAgentMerAccountRecordServiceImpl
 * @author: HuRui
 * @date: 2024/8/1
 **/
@Service("agentMerAccountRecordService")
public class BsAgentMerAccountRecordServiceImpl implements BsAgentMerAccountRecordService {
    @Resource
    private BsAgentMerAccountRecordMapper agentMerAccountRecordMapper;
    @Resource
    private UserCenter userCenter;

    @Override
    public void create(BsAgentMerAccountRecord 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());
        agentMerAccountRecordMapper.insert(data);
    }

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

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merNo"))) {
            criteria.andMerNoEqualTo(MapUtils.getString(param, "merNo"));
        }

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

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