package com.mvc.service.dic;

import com.mvc.dao.DicCustomizeMenuMapper;
import com.mvc.entity.*;
import com.mvc.model.DicCustomizeMenuModel;
import com.mvc.model.FacUserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DicCustomizeMenuService {

    @Resource
    private DicCustomizeMenuMapper dicCustomizeMenuMapper;

    public List<DicCustomizeMenuModel> findAllDicCustomizeMenuByUserId(Integer userId) {
        DicCustomizeMenuExample example = new DicCustomizeMenuExample();
        DicCustomizeMenuExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return selectByExample(example);
    }

    public DicCustomizeMenuModel saveDicCustomizeMenu(FacUserModel usermodel, TDynamicrouter dynamicrouter) {
        DicCustomizeMenu dicCustomizeMenu = new DicCustomizeMenu();
        dicCustomizeMenu.setUserId(usermodel.getId());
        dicCustomizeMenu.setRouterId(dynamicrouter.getId());
        dicCustomizeMenu.setCreateTime(new Date());

        DicCustomizeMenuModel model = new DicCustomizeMenuModel();
        dicCustomizeMenuMapper.insert(dicCustomizeMenu);
        model.setId(dicCustomizeMenu.getId());
        return model;
    }

    public int deleteDicCustomizeMenu(FacUserModel usermodel, TDynamicrouter dynamicrouter) {
        DicCustomizeMenuExample example = new DicCustomizeMenuExample();
        DicCustomizeMenuExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(usermodel.getId());
        criteria.andRouterIdEqualTo(dynamicrouter.getId());
        return dicCustomizeMenuMapper.deleteByExample(example);
    }

    private List<DicCustomizeMenuModel> selectByExample(DicCustomizeMenuExample example) {
        List<DicCustomizeMenu> dicCustomizeMenus = dicCustomizeMenuMapper.selectByExample(example);

        List<DicCustomizeMenuModel> resList = new ArrayList<>();
        for (DicCustomizeMenu dicCustomizeMenu : dicCustomizeMenus) {
            DicCustomizeMenuModel model = new DicCustomizeMenuModel();
            BeanUtils.copyProperties(dicCustomizeMenu, model);
            resList.add(model);
        }

        return resList;
    }

    public String checkDataLegitimacy(Integer userId, Integer routerId, int operationType) {
        DicCustomizeMenuExample example = new DicCustomizeMenuExample();
        DicCustomizeMenuExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andRouterIdEqualTo(routerId);
        List<DicCustomizeMenuModel> dicCustomizeMenus = selectByExample(example);
        String msg = "";

        if (operationType == 0) {//增
            if (dicCustomizeMenus.size() > 0) {
                msg = "快捷菜单中已存在此功能";
            }
        } else if (operationType == 1) {//删
            if (dicCustomizeMenus.size() == 0) {
                msg = "快捷菜单中已无此功能,刷新或重新登录生效,请勿重复操作";
            }
        }

        return msg;
    }
}
