package com.mvc.service.app;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.OperationType;
import com.mvc.dao.AppUserMapper;
import com.mvc.entity.AppUser;
import com.mvc.entity.AppUserExample;
import com.mvc.interceptor.JwtInfoMap;
import com.mvc.model.AppUserModel;
import com.mvc.model.DicConfigAuthModel;
import com.mvc.model.TAffiliatedunitModel;
import com.mvc.service.TAffiliatedunitService;
import com.mvc.service.dic.DicConfigAuthService;
import com.mvc.service.log.OperationLogService;
import com.mvc.utils.JwtUtil;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class AppUserService {
    @Resource
    private AppUserMapper appUserMapper;

    @Resource
    private DicConfigAuthService dicConfigAuthService;

    @Resource
    private TAffiliatedunitService tAffiliatedunitService;

    @Resource
    private OperationLogService operationLogService;

    public Map<String, Object> findAllAppUser(AppUserModel queryModel) {
        Map<String, Object> resMap = new HashMap<>();
        AppUserExample example = new AppUserExample();
        AppUserExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getUserName())) {
            criteria.andUserNameLike("%" + queryModel.getUserName() + "%");
        }
        if (StringUtils.isNotEmpty(queryModel.getLoginId())) {
            criteria.andLoginIdLike("%" + queryModel.getLoginId() + "%");
        }
        if(StringUtils.isNotEmpty(queryModel.getUserType())){
            criteria.andUserTypeEqualTo(queryModel.getUserType());
        }
        if(StringUtils.isNotEmpty(queryModel.getUnitCode())){
            criteria.andUnitCodeEqualTo(queryModel.getUnitCode());
        }
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());
        List<AppUser> AppUsers = appUserMapper.selectByExample(example);
        List<AppUserModel> resList = new ArrayList<>();
        for (AppUser AppUser : AppUsers) {
            AppUserModel model = new AppUserModel();
            BeanUtils.copyProperties(AppUser, model);

            if (!io.netty.util.internal.StringUtil.isNullOrEmpty(model.getConfigAuthId())) {
                DicConfigAuthModel authModel = dicConfigAuthService.findDicConfigAuthByAuthId(model.getConfigAuthId());
                model.setDicConfigAuthModel(authModel);
            }
            if(StringUtils.isNotEmpty(model.getUnitCode())){
                TAffiliatedunitModel unitModel = tAffiliatedunitService.findTAffiliatedunit(model.getUnitCode());
                if(unitModel!=null){
                    model.setUnitName(unitModel.getUnitName());
                }
            }
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public Map<String, Object> findAllAppUser(Integer page, Integer rows) {
        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(page, rows);
        List<AppUser> AppUsers = appUserMapper.selectByExample(null);
        List<AppUserModel> resList = new ArrayList<>();
        for (AppUser AppUser : AppUsers) {
            AppUserModel model = new AppUserModel();
            BeanUtils.copyProperties(AppUser, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public AppUserModel saveAppUser(AppUserModel model) {
        model.setUserId(StringUtil.getUuid());
        model.setCreateTime(new Date());

        if(io.netty.util.internal.StringUtil.isNullOrEmpty(model.getHighAuthFlag())){
            model.setHighAuthFlag("0");
        }

        AppUser AppUser = new AppUser();
        BeanUtils.copyProperties(model, AppUser);
        int insert = appUserMapper.insert(AppUser);
        model.setId(AppUser.getId());
        return model;
    }

    /**
     * 根据登陆ID获取对象
     * @param loginid
     * @return
     */
    public AppUserModel getModelByLoginid(String loginid){
        AppUserExample example = new AppUserExample();
        AppUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginIdEqualTo(loginid);
        List<AppUser> appUsers = appUserMapper.selectByExample(example);
        if(appUsers!=null&&appUsers.size()>0){
            AppUserModel model=new AppUserModel();
            BeanUtils.copyProperties(appUsers.get(0), model);
            return model;
        }
        return null;
    }

    /**
     * 根据userId获取对象
     * @param userId
     * @return
     */
    public AppUserModel getModelByUserId(String userId){
        AppUserExample example = new AppUserExample();
        AppUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<AppUser> appUsers = appUserMapper.selectByExample(example);
        if(appUsers!=null&&appUsers.size()>0){
            AppUserModel model=new AppUserModel();
            BeanUtils.copyProperties(appUsers.get(0), model);
            return model;
        }
        return null;
    }

    public AppUserModel updateAppUser(AppUserModel model) {
        AppUser AppUser = appUserMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, AppUser);
        int i = appUserMapper.updateByPrimaryKey(AppUser);
        return model;
    }

    public int deleteAppUser(int id) {
        int i = appUserMapper.deleteByPrimaryKey(id);
        return i;
    }

    //app用户登录接口
    public AppUserModel loginAppUser(String loginId, String loginPwd) {
        AppUserExample example = new AppUserExample();
        AppUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginIdEqualTo(loginId);
        criteria.andLoginPwdEqualTo(loginPwd);
        criteria.andUseFlagEqualTo("1");        //控制在用状态  2022-07-01 guozhao
        List<AppUser> appUsers = appUserMapper.selectByExample(example);
        if (appUsers.size() > 0) {
            AppUserModel model = new AppUserModel();
            BeanUtils.copyProperties(appUsers.get(0), model);

            if (!io.netty.util.internal.StringUtil.isNullOrEmpty(model.getConfigAuthId())) {
                model.setDicConfigAuthModel(dicConfigAuthService.findDicConfigAuthByAuthId(model.getConfigAuthId()));
            }
            operationLogService.saveOperationLog(3, OperationType.LOGIN,model,"");

            Map<String, Object> info = new JwtInfoMap(model);
            model.setAccessToken(JwtUtil.sign(model.getUserId(), info));
            return model;
        } else {
            return null;
        }
    }
}
