package com.yh.saas.plugin.operation.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.winsea.svc.base.base.util.IDUtils;
import com.winsea.svc.base.security.entity.Account;
import com.winsea.svc.base.security.service.IAccountService;
import com.yh.saas.common.support.util.query.QueryUtils;
import com.yh.saas.common.support.util.uniqueness.UniquenessChecker;
import com.yh.saas.plugin.operation.entity.CommonOperator;
import com.yh.saas.plugin.operation.mapper.OperatorMapper;
import com.yh.saas.plugin.operation.service.IOperatorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 账户信息 服务实现类
 * </p>
 *
 * @author zhangb
 */
@Service
public class OperatorPluginServiceImpl extends ServiceImpl<OperatorMapper, CommonOperator> implements IOperatorService {

	/*@SofaReference
	private SupportEventBus eventBus;*/

    @Autowired
    private OperatorMapper operatorMapper;

    @SofaReference
    private IAccountService accountService;

    @Transactional
    @Override
    public void create(CommonOperator operator) {
        // 通过账户手机号判断该账户是否存在
        Wrapper<CommonOperator> queryConflictWrapper = new EntityWrapper<CommonOperator>().eq(CommonOperator.QueryFields.OPERATOR_MOBILE_PHONE,
                operator.getOperatorMobilePhone()).or().eq(CommonOperator.QueryFields.OPERATOR_NAME, operator.getOperatorName());
        UniquenessChecker.check(this.baseMapper, queryConflictWrapper, operator);

        // 创建账户
        operator.setOperatorId(IDUtils.generateId(IDUtils.IDType.UUID, null));
        operator.setEnabled(1);
        this.insert(operator);

        // 生成账户
        generateOperatorAccount(operator);

    }

    @Transactional
    @Override
    public void changeInfo(CommonOperator operator) {
        // 冲突字段检测
        Wrapper<CommonOperator> queryConflictWrapper = new EntityWrapper<CommonOperator>().eq(CommonOperator.QueryFields.OPERATOR_MOBILE_PHONE,
                operator.getOperatorMobilePhone()).or().eq(CommonOperator.QueryFields.OPERATOR_NAME, operator.getOperatorName());
        UniquenessChecker.check(this.baseMapper, queryConflictWrapper, operator);

        // 更新账户信息
        if (StringUtils.isNotEmpty(operator.getOperatorId())) {
            operator.setEnabled(null);
            this.updateAllColumnById(operator);
        }

        Account account = new Account();
        account.setAccountId(operator.getOperatorId());

        // 如果更改了手机号则重置账户账户绑定的手机号
        if (null != operator.getOperatorMobilePhone()) {
            account.setUserMobilePhone(operator.getOperatorMobilePhone());
        }

        //同步登录名
        if (null != operator.getOperatorName()) {
            account.setLoginName(operator.getOperatorName());
        }

        accountService.changeInfo(account);

        //同步密码
        if (null != operator.getLoginPassword()) {
            accountService.changePassword(operator.getOperatorId(), operator.getLoginPassword());
        }

    }


    @Transactional
    @Override
    public void enable(String operatorId) {
        CommonOperator operator = selectById(operatorId);
        operator.setEnabled(1);
        this.updateById(operator);
        accountService.enable(operatorId);
    }

    @Transactional
    @Override
    public void disable(String operatorId) {
        CommonOperator operator = selectById(operatorId);
        operator.setEnabled(0);
        this.updateById(operator);
        accountService.disable(operatorId);
    }

    @Transactional
    @Override
    public void remove(String operatorId) {
        // 删除账户
        deleteById(operatorId);

        // 解绑账户资源
        accountService.remove(operatorId);
    }

    @Override
    public CommonOperator selectById(String id) {
        return operatorMapper.selectById(id);
    }

    @Override
    public Page<CommonOperator> findOperators(String keyword, Page<CommonOperator> operator) {
        return selectPage(operator, QueryUtils.generateFuzzySearchWrapper(keyword, CommonOperator.class));
    }

    /**
     * 生成账户
     *
     * @param operator 账户信息
     * @return 账户信息
     */
    private void generateOperatorAccount(CommonOperator operator) {
        Account account = new Account();
        account.setLoginName(operator.getOperatorName());
        account.setLoginPassword(operator.getLoginPassword());
        account.setUserId(operator.getOperatorId());
        account.setUserMobilePhone(operator.getOperatorMobilePhone());
        account.setAccountType(Account.AccountType.OPERATOR);
        account.setEnabled(operator.getEnabled());
        accountService.create(account);
    }
}
