package com.mvc.service.dic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.OperationType;
import com.mvc.dao.FacUserMapper;
import com.mvc.entity.FacUser;
import com.mvc.entity.FacUserExample;
import com.mvc.model.FacUserModel;
import com.mvc.model.FacUserRoleModel;
import com.mvc.service.log.OperationLogService;
import com.mvc.utils.StringUtil;
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.*;

@Service
@Transactional
public class FacUserService {

    @Resource
    private FacUserMapper facUserMapper;

    @Resource
    private FacUserRoleService facUserRoleService;

    @Resource
    private OperationLogService operationLogService;

    public Map<String, Object> findAllFacUser(FacUserModel queryModel) {
        Map<String, Object> resMap = new HashMap<>();
        List<FacUserModel> resList = new ArrayList<>();
        FacUserModel facUserModel = findFacUserByUserId(queryModel.getUserId());
        if (facUserModel == null) {
            resMap.put("total", 0);
            resMap.put("list", resList);
            return resMap;
        } else {
            if (facUserModel.getAdminFlag() == null || !facUserModel.getAdminFlag().equals("1")) {//查询的用户不是管理员,只返回自己的数据
                resList.add(facUserModel);
                resMap.put("total", 1);
                resMap.put("list", resList);
                return resMap;
            }
        }

        FacUserExample example = new FacUserExample();
        FacUserExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getUserName())) {
            criteria.andUserNameLike("%" + queryModel.getUserName() + "%");
        }
        example.setOrderByClause("ID desc");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());
        List<FacUser> FacUsers = facUserMapper.selectByExample(example);
        for (FacUser FacUser : FacUsers) {
            FacUserModel model = new FacUserModel();
            BeanUtils.copyProperties(FacUser, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public FacUserModel saveFacUser(FacUserModel model) {
        model.setUserId(StringUtil.getUuid());
        model.setCreateTime(new Date());
        if (StringUtils.isEmpty(model.getAdminFlag())) {
            model.setAdminFlag("0");
        }
        FacUser FacUser = new FacUser();
        //设置角色名称
        if (StringUtils.isNotEmpty(model.getRoleId())) {
            FacUserRoleModel roleModel = facUserRoleService.findFacUserRoleModelByRoleId(model.getRoleId());
            if (roleModel != null) {
                model.setRoleName(roleModel.getRoleName());
            }
        }
        BeanUtils.copyProperties(model, FacUser);
        int insert = facUserMapper.insert(FacUser);
        model.setId(FacUser.getId());
        return model;
    }

    public FacUserModel updateFacUser(FacUserModel model) {
        FacUser FacUser = facUserMapper.selectByPrimaryKey(model.getId());
        //设置角色名称
        if (StringUtils.isNotEmpty(model.getRoleId())) {
            FacUserRoleModel roleModel = facUserRoleService.findFacUserRoleModelByRoleId(model.getRoleId());
            if (roleModel != null) {
                model.setRoleName(roleModel.getRoleName());
            }
        }
        BeanUtils.copyProperties(model, FacUser);
        int i = facUserMapper.updateByPrimaryKey(FacUser);
        return model;
    }

    public int deleteFacUser(int id) {
        int i = facUserMapper.deleteByPrimaryKey(id);
        return i;
    }

    //内部用户登录接口
    public FacUserModel loginFacuser(String loginId) {
        FacUserExample example = new FacUserExample();
        FacUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginIdEqualTo(loginId);
        //criteria.andLoginPwdEqualTo(loginPwd);
        List<FacUser> facUsers = facUserMapper.selectByExample(example);
        if (facUsers.size() > 0) {
            FacUserModel model = new FacUserModel();
            BeanUtils.copyProperties(facUsers.get(0), model);
            operationLogService.saveOperationLog(0, OperationType.LOGIN,model,"");
            return model;
        } else {
            return null;
        }
    }

    /**
     * 根据Userid(业务id)获取用户对象
     *
     * @param userId
     * @return
     */
    public FacUserModel findFacUserByUserId(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        FacUserExample example = new FacUserExample();
        FacUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<FacUser> facUsers = facUserMapper.selectByExample(example);
        if (facUsers.size() > 0) {
            FacUserModel model = new FacUserModel();
            BeanUtils.copyProperties(facUsers.get(0), model);
            return model;
        }
        return null;
    }

    /**
     * 根据登陆ID获取内部用户对象(判重使用)
     *
     * @param loginId
     * @return
     */
    public FacUser findFacUserByLoginId(String loginId) {
        if (StringUtils.isEmpty(loginId)) {
            return null;
        }
        FacUserExample example = new FacUserExample();
        FacUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginIdEqualTo(loginId);
        List<FacUser> facUsers = facUserMapper.selectByExample(example);

        return facUsers.size() > 0 ? facUsers.get(0) : null;
    }
}
