package com.hfkj.service.merchant.impl;

import com.hfkj.common.security.UserCenter;
import com.hfkj.dao.BsMerchantChainBrandAccountRecordMapper;
import com.hfkj.entity.BsMerchantChainBrandAccountRecord;
import com.hfkj.entity.BsMerchantChainBrandAccountRecordExample;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.merchant.BsMerchantChainBrandAccountRecordService;
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: BsMerchantChainBrandAccountRecordServiceImpl
 * @author: HuRui
 * @date: 2024/8/14
 **/
@Service("merchantChainBrandAccountRecordService")
public class BsMerchantChainBrandAccountRecordServiceImpl implements BsMerchantChainBrandAccountRecordService {
    @Resource
    private BsMerchantChainBrandAccountRecordMapper merchantChainBrandAccountRecordMapper;
    @Resource
    private UserCenter userCenter;
    @Override
    public void create(BsMerchantChainBrandAccountRecord 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());
        merchantChainBrandAccountRecordMapper.insert(data);
    }

    @Override
    public BsMerchantChainBrandAccountRecord getDetail(Long id) {
        return merchantChainBrandAccountRecordMapper.selectByPrimaryKey(id);
    }

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

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

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

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

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