package com.younike.system.service.impl;

import com.younike.common.core.domain.entity.SysUser;
import com.younike.common.exception.ServiceException;
import com.younike.common.utils.SecurityUtils;
import com.younike.common.utils.StringUtils;
import com.younike.common.utils.bean.BeanUtils;
import com.younike.system.domain.Merchant;
import com.younike.system.domain.MerchantBank;
import com.younike.system.domain.MerchantInfo;
import com.younike.system.domain.vo.MerchantModifyVO;
import com.younike.system.domain.vo.MerchantSettledResultVO;
import com.younike.system.domain.vo.MerchantSettledVO;
import com.younike.system.enums.MerchantStatusEnum;
import com.younike.system.enums.UserTypeEnum;
import com.younike.system.mapper.MerchantMapper;
import com.younike.system.param.MerchantQueryParam;
import com.younike.system.service.IMerchantBankService;
import com.younike.system.service.IMerchantInfoService;
import com.younike.system.service.IMerchantService;
import com.younike.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import static com.younike.system.enums.MerchantStatusEnum.AUDITING;

/**
 * 店铺信息Service业务层处理
 *
 * @author elev
 * @date 2022-01-05
 */
@Service
public class MerchantServiceImpl implements IMerchantService {
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMerchantInfoService merchantInfoService;
    @Autowired
    private IMerchantBankService merchantBankService;

    public final static int DEFAULT_PASSWORD_LENGTH = 6;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MerchantSettledResultVO settled(MerchantSettledVO merchantSettledVO) {
        // 1. 注册用户并返回账号信息
        SysUser user = merchantSettledVO.getUser();
        String account = user.getPhonenumber();
        if (Objects.nonNull(sysUserService.selectUserByUserName(account))){
            throw new ServiceException("该手机已注册过账号，无法重复注册");
        }
        user.setUserName(account);
        String password = StringUtils.getCode(DEFAULT_PASSWORD_LENGTH);
        user.setPassword(SecurityUtils.encryptPassword(password));
        boolean isMerchant = 0 == merchantSettledVO.getMerchantType();
        user.setUserType(isMerchant ? UserTypeEnum.MERCHANT.getCode(): UserTypeEnum.PERSONAL.getCode());
        sysUserService.registerUser(user);

        // 2. 店铺信息保存
        String createBy = user.getUserId().toString();
        merchantSettledVO.setAdminId(user.getUserId());
        BeanUtils.copyBeanProp(merchantSettledVO, merchantSettledVO.getAddress());
        merchantSettledVO.setMerchantPhone(account);
        merchantSettledVO.setServerPhone(account);
        merchantSettledVO.setCreateBy(createBy);
        merchantSettledVO.setShopState(AUDITING.getCode());
        merchantMapper.insertMerchant(merchantSettledVO);

        Long merchantId = merchantSettledVO.getMerchantId();
        MerchantInfo merchantInfo = merchantSettledVO.getInfo();
        merchantInfo.setMerchantId(merchantId);
        merchantInfo.setCreateBy(createBy);
        merchantInfoService.insertMerchantInfo(merchantInfo);
        MerchantBank merchantBank = merchantSettledVO.getBank();
        merchantBank.setMerchantId(merchantId);
        merchantBank.setCreateBy(createBy);
        merchantBankService.insertMerchantBank(merchantBank);

        MerchantSettledResultVO result = new MerchantSettledResultVO();
        result.setUserName(account.replaceAll("(\\\\d{3})\\\\d*(\\\\d{4})", "$1****$2"));
        result.setPassword(password);
        return result;
    }

    @Override
    public int settledModify(MerchantSettledVO merchantSettledVO) {
        merchantInfoService.updateMerchantInfo(merchantSettledVO.getInfo());
        merchantBankService.updateMerchantBank(merchantSettledVO.getBank());
        merchantSettledVO.setShopState(AUDITING.getCode());
        return merchantMapper.updateMerchant(merchantSettledVO);
    }

    @Override
    public int merchantModify(MerchantModifyVO merchantModifyVO) {
        Merchant merchant = new Merchant();
        BeanUtils.copyBeanProp(merchant, merchantModifyVO);
        return merchantMapper.updateMerchant(merchant);
    }

    @Override
    public Merchant selectMerchantByAdminId(Long adminId) {
        return merchantMapper.selectMerchantByAdminId(adminId);
    }

    /**
     * 查询店铺信息
     *
     * @param merchantId 店铺信息主键
     * @return 店铺信息
     */
    @Override
    public Merchant selectMerchantByMerchantId(Long merchantId) {
        return merchantMapper.selectMerchantByMerchantId(merchantId);
    }

    /**
     * 查询店铺信息列表
     *
     * @param merchant 店铺信息
     * @return 店铺信息
     */
    @Override
    public List<Merchant> selectMerchantList(Merchant merchant) {
        return merchantMapper.selectMerchantList(merchant);
    }

    @Override
    public List<MerchantSettledVO> selectAuditMerchantList(MerchantQueryParam param) {
        return merchantMapper.selectAuditMerchantList(param);
    }

    @Override
    public List<MerchantSettledVO> selectPassMerchantList(MerchantQueryParam param) {
        return merchantMapper.selectPassMerchantList(param);
    }

    /**
     * 查询店铺入驻详情
     *
     * @param merchantId 店铺信息主键
     * @return 店铺信息
     */
    @Override
    public MerchantSettledVO merchantInfo(Long merchantId) {
        MerchantSettledVO merchantSettledVO = new MerchantSettledVO();
        Merchant merchant = merchantMapper.selectMerchantByMerchantId(merchantId);
        if (merchant != null) {
            BeanUtils.copyBeanProp(merchantSettledVO, merchant);
            if (merchant.getAdminId() != null) {
                merchantSettledVO.setUser(sysUserService.selectUserById(merchant.getAdminId()));
            }
            MerchantInfo merchantInfo = merchantInfoService.selectMerchantInfoByMerchantId(merchantId);
            MerchantBank merchantBank = merchantBankService.selectMerchantBankByMerchantId(merchantId);
            SysUser user = sysUserService.selectSysUserById(merchant.getAdminId());
            merchantSettledVO.setInfo(merchantInfo == null ? new MerchantInfo() : merchantInfo);
            merchantSettledVO.setBank(merchantBank == null ? new MerchantBank() : merchantBank);
            merchantSettledVO.setUser(user);
        }
        return merchantSettledVO;
    }

    /**
     * 修改店铺信息
     *
     * @param merchant 店铺信息
     * @return 结果
     */
    @Override
    public int updateMerchant(Merchant merchant) {
        merchant.setUpdateTime(LocalDateTime.now());
        return merchantMapper.updateMerchant(merchant);
    }

    /**
     * 批量删除店铺信息
     *
     * @param merchantIds 需要删除的店铺信息主键
     * @return 结果
     */
    @Override
    public int deleteMerchantByMerchantIds(Long[] merchantIds) {
        return merchantMapper.deleteMerchantByMerchantIds(merchantIds);
    }

    /**
     * 删除店铺信息信息
     *
     * @param merchantId 店铺信息主键
     * @return 结果
     */
    @Override
    public int deleteMerchantByMerchantId(Long merchantId)
    {
        return merchantMapper.deleteMerchantByMerchantId(merchantId);
    }

    @Override
    public void updateMerchants(List<Merchant> merchantList) {
        merchantMapper.updateMerchants(merchantList);
    }
}
