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.dao.HighFleetOilCardUserMapper;
import com.hai.entity.*;
import com.hai.enum_type.UserCardType;
import com.hai.service.HighFleetOilCardUserService;
import com.hai.service.HighUserCardService;
import com.hai.service.HighUserService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
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: HighFleetOilCardUserServiceImpl
 * @author: HuRui
 * @date: 2022/12/12
 **/
@Service("fleetOilCardUserService")
public class HighFleetOilCardUserServiceImpl implements HighFleetOilCardUserService {

    @Resource
    private HighFleetOilCardUserMapper fleetOilCardUserMapper;

    @Resource
    private HighUserService userService;

    @Resource
    private HighUserCardService userCardService;

    @Override
    public void editUser(HighFleetOilCardUser fleetOilCardUser) {
        if (fleetOilCardUser.getId() == null) {
            fleetOilCardUser.setCreateTime(new Date());
            fleetOilCardUser.setUpdateTime(new Date());
            fleetOilCardUserMapper.insert(fleetOilCardUser);
        } else {
            fleetOilCardUser.setUpdateTime(new Date());
            fleetOilCardUserMapper.updateByPrimaryKey(fleetOilCardUser);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void importData(HighFleetOilCard fleetOilCard, List<HighFleetOilCardUser> fleetOilCardUserList) throws Exception {
        for (HighFleetOilCardUser fleetOilCardUser : fleetOilCardUserList) {
            if (StringUtils.isBlank(fleetOilCardUser.getContactName())
                || StringUtils.isBlank(fleetOilCardUser.getContactPhone())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            fleetOilCardUser.setFleetOilCardId(fleetOilCard.getId());
            fleetOilCardUser.setFleetOilCardNo(fleetOilCard.getCardNo());
            fleetOilCardUser.setAmountConsume(new BigDecimal("0"));
            fleetOilCardUser.setStatus(1);
            editUser(fleetOilCardUser);

            // 查询用户信息
            HighUser user = userService.findByPhone(fleetOilCardUser.getContactPhone());
            if (user == null) {
                user = new HighUser();
                user.setName(fleetOilCardUser.getContactName());
                user.setPhone(fleetOilCardUser.getContactPhone());
                user.setRegTime(new Date());
                user.setGold(0);
                user.setStatus(1);
                // 用户信息完整状态
                // 0：完整
                // 1：需要填写用户信息
                // 2：需要填写手机号
                user.setInfoCompleteStatus(1);
                // TODO 需要修改
                userService.insertUser(user , null);
            }

            // 是否绑定油卡
            HighUserCard userCard = userCardService.getDetailByUserCardNo(user.getId(), fleetOilCard.getCardNo());
            if (userCard == null) {
                userCardService.bindCard(UserCardType.type3.getType(), fleetOilCard.getCardNo(), user.getId());
            }

        }
    }

    @Override
    public HighFleetOilCardUser getDetailById(Long id) {
        return fleetOilCardUserMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void delete(Long id) {
        HighFleetOilCardUser detail = getDetailById(id);
        if (detail == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        detail.setStatus(0);
        editUser(detail);

        // 查询用户信息
        HighUser user = userService.findByPhone(detail.getContactPhone());
        if (user != null) {
            HighUserCard userCard = userCardService.getDetailByUserCardNo(user.getId(), detail.getFleetOilCardNo());
            if (userCard != null) {
                userCard.setStatus(0);
                userCardService.editCard(userCard);
            }
        }
    }

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "fleetOilCardNo"))) {
            criteria.andFleetOilCardNoEqualTo(MapUtils.getString(param, "fleetOilCardNo"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "contactName"))) {
            criteria.andContactNameLike("%"+MapUtils.getString(param, "contactName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "contactPhone"))) {
            criteria.andContactPhoneLike("%"+MapUtils.getString(param, "contactPhone")+"%");
        }

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

    @Override
    public List<HighFleetOilCardUser> getUserListByCard(Long cardId) {
        HighFleetOilCardUserExample example = new HighFleetOilCardUserExample();
        example.createCriteria().andStatusNotEqualTo(0).andFleetOilCardIdEqualTo(cardId);
        example.setOrderByClause("create_time desc");
        return fleetOilCardUserMapper.selectByExample(example);
    }

    @Override
    public HighFleetOilCardUser getCardUser(String fleetCardNo, String contactPhone) {
        HighFleetOilCardUserExample example = new HighFleetOilCardUserExample();
        example.createCriteria()
                .andFleetOilCardNoEqualTo(fleetCardNo)
                .andContactPhoneEqualTo(contactPhone)
                .andStatusNotEqualTo(0);
        List<HighFleetOilCardUser> list = fleetOilCardUserMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
