package com.hfkj.service.merchant.impl;

import com.hfkj.common.security.UserCenter;
import com.hfkj.dao.BsMerchantAccountRecordMapper;
import com.hfkj.entity.BsMerchantAccountRecord;
import com.hfkj.entity.BsMerchantAccountRecordExample;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.merchant.BsMerchantAccountRecordService;
import com.hfkj.service.merchant.BsMerchantAccountService;
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: merchantAccountRecordServiceImpl
 * @author: HuRui
 * @date: 2024/7/2
 **/
@Service("merchantAccountRecord")
public class BsMerchantAccountRecordServiceImpl implements BsMerchantAccountRecordService {
    @Resource
    private BsMerchantAccountRecordMapper merchantAccountRecordMapper;
    @Resource
    private UserCenter userCenter;
    @Override
    public void create(BsMerchantAccountRecord data) {
        data.setOpUserType(0);
        data.setOpUserId(0L);
        data.setOpUserName("未知");
        data.setOpUserPhone("未知");

        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());
        merchantAccountRecordMapper.insert(data);
    }

    @Override
    public BsMerchantAccountRecord getDetail(Long id) {
        return merchantAccountRecordMapper.selectByPrimaryKey(id);
    }

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

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

        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.andSourceOrderNoLike("%"+MapUtils.getString(param, "sourceOrderNo")+"%");
        }

        example.setOrderByClause("create_time desc,id desc");
        return merchantAccountRecordMapper.selectByExample(example);
    }
}
