package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.dictionary.DicUserAuthState;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.UserMapper;
import com.lightinit.hsdataplatform.mapper.UserPersonalMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.IUserPersonalService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UserPersonalServiceImpl implements IUserPersonalService {
    @Autowired
    private UserPersonalMapper userPersonalMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<PersionalUserAuthListModel> getAuthUserList(PersionalUserSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return userPersonalMapper.selectModelList(inputModel);
    }

    @Override
    public long queryAuthUserCount(PersionalUserSearchModel inputModel) {
        return userPersonalMapper.selectModelListCount(inputModel);
    }

    @Override
    public PersionalUserAuthDataModel queryOne(Long id) {
        UserPersonal userPersonal = userPersonalMapper.selectByPrimaryKey(id);
        PersionalUserAuthDataModel persionalUserAuthDataModel = new PersionalUserAuthDataModel();
        BeanUtils.copyProperties(userPersonal,persionalUserAuthDataModel);
        User user = userMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(user,persionalUserAuthDataModel);
        persionalUserAuthDataModel.setPhone(userPersonal.getPhone());
        return persionalUserAuthDataModel;
    }

    @Override
    public List<UserRegisterListModel> getRegisterUserList(String phone, BaseExample.Page  page){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UserExample example = new UserExample();
        example.setPage(page);
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andAuthStateEqualTo(DicUserAuthState.APPROVED);
        if(StringUtils.isNotEmpty(phone)){
            criteria.andPhoneLike('%'+phone+'%');
        }
        List<User> users = userMapper.selectByExample(example);
        ArrayList<UserRegisterListModel> userRegisterListModels = new ArrayList<UserRegisterListModel>();
        UserRegisterListModel userRegisterListModel=null;
        for (User user : users) {
            userRegisterListModel=new UserRegisterListModel();
            BeanUtils.copyProperties(user,userRegisterListModel);
            userRegisterListModels.add(userRegisterListModel);
        }

        return userRegisterListModels;
    }
    @Override
    public long queryRegisterUserCount(String phone) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andAuthStateEqualTo(DicUserAuthState.APPROVED);
        if(StringUtils.isNotEmpty(phone)){
            criteria.andPhoneLike("%"+phone+"%");
        }
        return userMapper.countByExample(example);
    }

    @Override
    public List<RegisterUserStatisticsModel> registerUserStatistics() {
        return userMapper.registerUserStatistics();
    }

    @Override
    public ResultState registerUserGraph(String year) {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> monthNames = new ArrayList<>();
        ArrayList<Integer> registerSum = new ArrayList<>();
        Integer total=0;
        List<RegisterUserStatisticsModel> list=userMapper.registerUserGraph(year);
        for (RegisterUserStatisticsModel model : list) {
            if(model.getRegisteMonth()!=null){
                monthNames.add(model.getRegisteMonth()+"月");
                registerSum.add(model.getRegisteSum());
            }else{
                total=model.getRegisteSum();
            }
        }
        map.put("year",list.get(0).getRegisteYear());
        map.put("monthNames",monthNames);
        map.put("registerSum",registerSum);
        map.put("total",total);
        resultState.setData(map);
        return resultState;
    }

}
