package com.hfkj.service.merchant.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.MD5Util;
import com.hfkj.dao.BsMerchantChainBrandMapper;
import com.hfkj.entity.BsMerchantChainBrand;
import com.hfkj.entity.BsMerchantChainBrandAccount;
import com.hfkj.entity.BsMerchantChainBrandAccountExample;
import com.hfkj.entity.SecUser;
import com.hfkj.service.merchant.BsMerchantChainBrandAccountService;
import com.hfkj.service.merchant.BsMerchantChainBrandService;
import com.hfkj.service.sec.SecUserService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @className: BsMerchantChainBrandServiceImpl
 * @author: HuRui
 * @date: 2024/8/14
 **/
@Service("merchantChainBrandService")
public class BsMerchantChainBrandServiceImpl implements BsMerchantChainBrandService {
    @Resource
    private BsMerchantChainBrandMapper merchantChainBrandMapper;
    @Resource
    private BsMerchantChainBrandAccountService merchantChainBrandAccountService;
    @Resource
    private SecUserService secUserService;
    @Override
    public void editData(BsMerchantChainBrand data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            merchantChainBrandMapper.insert(data);
        } else {
            merchantChainBrandMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void create(BsMerchantChainBrand chainBrand) throws Exception {
        editData(chainBrand);

        // 余额账户
        BsMerchantChainBrandAccount account = new BsMerchantChainBrandAccount();
        account.setMerChainBrandId(chainBrand.getId());
        account.setAmount(new BigDecimal("0"));
        account.setAmountConsume(new BigDecimal("0"));
        account.setStatus(1);
        merchantChainBrandAccountService.editData(account);

        if (secUserService.getDetailByLoginName(chainBrand.getContactsTelephone()) != null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已存在，请更换");
        }

        // 登录账户
        SecUser secUser = new SecUser();
        secUser.setUserName(chainBrand.getName());
        secUser.setLoginName(chainBrand.getContactsTelephone());
        secUser.setPassword(MD5Util.encode("123654".getBytes()));
        secUser.setObjectType(SecUserObjectTypeEnum.type6.getCode());
        secUser.setObjectId(chainBrand.getId());
        secUser.setRoleId(7L);
        secUser.setStatus(1);
        secUser.setCreateTime(new Date());
        secUser.setUpdateTime(new Date());
        secUserService.editUser(secUser);
    }

    @Override
    public void update(BsMerchantChainBrand chainBrand) {
        editData(chainBrand);
    }

    @Override
    public BsMerchantChainBrand getDetail(Long id) {
        return merchantChainBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> getList(Map<String, Object> param) {
        return merchantChainBrandMapper.selectChainBrand(param);
    }
}
