package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.dictionary.DicOrderState;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicResourceType;
import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.mapper.*;
import com.lightinit.hsdataportal.model.UserInfoModel;
import com.lightinit.hsdataportal.service.IUserModuleService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/26.
 */
@Service
public class UserModuleServiceImpl implements IUserModuleService{
    @Autowired
    private UserModuleMapper userModuleMapper;

    @Autowired
    private OrderMapper orderMapper ;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ResourceFollowMapper resourceFollowMapper ;

    @Autowired
    private UserAccountMapper userAccountMapper ;

    public List<UserModule> QueryUserModuleList(long uid) {
        UserModuleExample example=new UserModuleExample();
        UserModuleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(uid);
        return userModuleMapper.selectByExample(example);
    }

    public boolean SaveUserModule(long uid, List<Long> ids) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UserModuleExample example=new UserModuleExample();
        UserModuleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(uid);
        userModuleMapper.deleteByExample(example);
        int resultCount=0;
        for (Long id:ids) {
            UserModule record=new UserModule();
            record.setModuleid(id);
            record.setUserid(uid);
            resultCount+=userModuleMapper.insert(record);
        }
        return resultCount==ids.size();
    }

    public boolean CheckUserHasModules(long uid) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UserModuleExample example=new UserModuleExample();
        UserModuleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(uid);
        return userModuleMapper.countByExample(example)>0;
    }

    @Override
    public UserInfoModel selectUserInfo(Long userId) {
        UserInfoModel userInfoModel = new UserInfoModel() ;
        //查询user表
        User user = userMapper.selectByPrimaryKey(userId) ;
        userInfoModel.setAuthState(user.getAuthState());
        //查询order_goods表 ，累计消费金额
        OrderExample example = new OrderExample() ;
        OrderExample.Criteria criteria = example.createCriteria() ;
        criteria.andPlatformTypeEqualTo(DicPlatformType.OPEN) ;
        criteria.andUserIdEqualTo(userId) ;
        criteria.andOrderStateEqualTo(DicOrderState.PAY_SUCCESS) ;
        List<Order> orders = orderMapper.selectByExample(example) ;
        BigDecimal bigDecimal = new BigDecimal(0) ;
        if (orders.size()>0){
        for (Order order : orders) {
            if (!StringUtils.isEmpty(order.getOrderAmount())) {
                bigDecimal = bigDecimal.add(order.getOrderAmount());
            }
        }
        }
        userInfoModel.setUserAllCount(bigDecimal);
        //api关注数量和服务关注数量
        userInfoModel.setFollowApiCount(resourceFollowMapper.selectFollowApiOrMicCount(DicPlatformType.OPEN,userId, DicResourceType.API));
        userInfoModel.setFollowMicCount(resourceFollowMapper.selectFollowApiOrMicCount(DicPlatformType.OPEN,userId, DicResourceType.MICROAPP));
        //查询user_account账户表
        UserAccount userAccount = userAccountMapper.selectByPrimaryKey(userId) ;
        BigDecimal maney = new BigDecimal(0) ;
        if (!StringUtils.isEmpty(userAccount)){
            maney  = userAccount.getBalance() ;
        }
        userInfoModel.setSurplus(maney);
        return userInfoModel;
    }
}
