package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.MD5Util;
import com.hai.dao.HighMerchantMapper;
import com.hai.entity.HighMerchant;
import com.hai.entity.HighMerchantExample;
import com.hai.entity.SecUser;
import com.hai.model.HighMerchantModel;
import com.hai.service.HighMerchantService;
import com.hai.service.SecUserService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service("highMerchantService")
public class HighMerchantServiceImpl implements HighMerchantService {

    @Resource
    private HighMerchantMapper highMerchantMapper;

    @Resource
    private SecUserService secUserService;

    @Override
    @Transactional
    public void insertMerchant(HighMerchantModel highMerchant) throws Exception {
        highMerchantMapper.insert(highMerchant);

        highMerchant.getSecUser().setUserName(highMerchant.getMerchantName());
        highMerchant.getSecUser().setLoginName(highMerchant.getSecUser().getTelephone());
        highMerchant.getSecUser().setPassword(MD5Util.encode(highMerchant.getSecUser().getPassword().getBytes()));
        highMerchant.getSecUser().setAdminFlag(1);
        highMerchant.getSecUser().setStatus(1);
        highMerchant.getSecUser().setRoleId(3L);
        highMerchant.getSecUser().setObjectType(2);
        highMerchant.getSecUser().setObjectId(highMerchant.getId());
        highMerchant.getSecUser().setCreateTime(new Date());
        highMerchant.getSecUser().setUpdateTime(new Date());
        secUserService.addUser(highMerchant.getSecUser());
    }

    @Override
    @Transactional
    public void updateMerchant(HighMerchantModel highMerchant) throws Exception {
        highMerchantMapper.updateByPrimaryKey(highMerchant);

        // 查询主账号
        SecUser mainAccount = secUserService.getMainAccount(2, highMerchant.getId());
        if (mainAccount != null) {
            String userPass = MD5Util.encode(highMerchant.getSecUser().getPassword().getBytes());
            if (!mainAccount.getPassword().equals(userPass)) {
                mainAccount.setPassword(userPass);
            }
            mainAccount.setTelephone(highMerchant.getTelephone());
            secUserService.updateUser(mainAccount);
        }
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        // 查询商户
        HighMerchantModel merchant = getMerchantById(id);
        if(merchant == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.MERCHANT_NOF_FOUND, "");
        }
        merchant.setStatus(status);
        highMerchantMapper.updateByPrimaryKey(merchant);
    }

    @Override
    public HighMerchantModel getMerchantById(Long id) {
        HighMerchant merchant = highMerchantMapper.selectByPrimaryKey(id);
        if (merchant == null) {
            return null;
        }
        HighMerchantModel merchantModel = new HighMerchantModel();
        BeanUtils.copyProperties(merchant, merchantModel);

        // 查询商户主账号
        SecUser mainAccount = secUserService.getMainAccount(2, merchant.getId());
        if (mainAccount != null) {
            merchantModel.setSecUser(mainAccount);
        }
        return merchantModel;
    }

    @Override
    public HighMerchant getDetailById(Long id) {
        return highMerchantMapper.selectByPrimaryKey(id);
    }

    @Override
    public HighMerchantModel getDetailByKey(String key) {
        HighMerchantExample example = new HighMerchantExample();
        example.createCriteria().andMerchantKeyEqualTo(key).andStatusEqualTo(1);
        List<HighMerchant> list = highMerchantMapper.selectByExample(example);
        if (list.size() > 0) {
            return getMerchantById(list.get(0).getId());
        }
        return null;
    }

    @Override
    public List<HighMerchant> getMerchantList(Map<String, Object> map) {
        HighMerchantExample example = new HighMerchantExample();
        HighMerchantExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLongValue(map, "companyId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "merchantKey"))) {
            criteria.andMerchantKeyEqualTo(MapUtils.getString(map, "merchantKey"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "merchantName"))) {
            criteria.andMerchantNameLike("%" + MapUtils.getString(map, "merchantName") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "telephone"))) {
            criteria.andTelephoneEqualTo(MapUtils.getString(map, "telephone"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "status"))) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        }

        // todo
        if (MapUtils.getBoolean(map, "ext1") != null && MapUtils.getBoolean(map, "ext1") == true) {
            criteria.andExt1IsNull();
        }

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