package com.reyun.saas.mob.user.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.reyun.saas.common.restResult.RestResult;
import com.reyun.saas.common.restResult.ResultGenerator;
import com.reyun.saas.mob.user.dao.ViewColumnMapper;
import com.reyun.saas.mob.user.dao.ViewColumnUserMapper;
import com.reyun.saas.mob.user.domain.ViewColumn;
import com.reyun.saas.mob.user.domain.ViewColumnUser;
import com.reyun.saas.mob.user.service.ViewColumnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

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

/**
 * @desc
 * @date
 */
@Slf4j
@Service
@Transactional
public class ViewColumnServiceImpl implements ViewColumnService {

    @Resource
    private ViewColumnMapper viewColumnMapper;
    @Resource
    private ViewColumnUserMapper viewColumnUserMapper;

    @Override
    public RestResult find(String menu, String os, Long loginUserId) {
        //查所有指标
        Example viewColumnExample = new Example(ViewColumn.class);
        if("1".equals(os)){
            viewColumnExample.and().andEqualTo("menu",menu).andNotEqualTo("genre","risk").orIsNull("genre");
        }else{//安卓才有风险指标
            viewColumnExample.and().andEqualTo("menu",menu);

        }
        List<ViewColumn> viewColumns = viewColumnMapper.selectByExample(viewColumnExample);
        //查用户已保存指标
        Example viewColumnUserExample = new Example(ViewColumnUser.class);
        viewColumnUserExample.and().andEqualTo("menu",menu).andEqualTo("userId",loginUserId).andEqualTo("os",os);
        ViewColumnUser viewColumnUser = viewColumnUserMapper.selectOneByExample(viewColumnUserExample);
        if(viewColumnUser!=null){//未保存不用处理
            Iterable<String> viewcolumnList = Splitter.on(",").omitEmptyStrings().trimResults().split(viewColumnUser.getViewcolumn());
            List<String> fieldCodeList = Lists.newArrayList(viewcolumnList);
            for (ViewColumn viewColumn : viewColumns) {
                if(viewColumn.getIsedit()){
                    if(fieldCodeList.contains(viewColumn.getFieldcode())){
                        viewColumn.setIscheck(true);
                    }else{
                        viewColumn.setIscheck(false);
                    }
                }
            }
        }
        return ResultGenerator.genSuccessResult(viewColumns);
    }

    @Override
    public RestResult save(String menu, String os, String viewcolumn, Long loginUserId) {
        //查用户已保存指标
        Example viewColumnUserExample = new Example(ViewColumnUser.class);
        viewColumnUserExample.and().andEqualTo("menu",menu).andEqualTo("userId",loginUserId).andEqualTo("os",os);
        ViewColumnUser viewColumnUser = viewColumnUserMapper.selectOneByExample(viewColumnUserExample);
        if(viewColumnUser!=null){
            viewColumnUser.setViewcolumn(viewcolumn);
            viewColumnUserMapper.updateByPrimaryKey(viewColumnUser);
        }else{
            viewColumnUser = new ViewColumnUser();
            viewColumnUser.setViewcolumn(viewcolumn);
            viewColumnUser.setMenu(menu);
            viewColumnUser.setUserId(loginUserId);
            viewColumnUser.setOs(os);
            viewColumnUserMapper.insert(viewColumnUser);
        }

        return ResultGenerator.genSuccessResult();

    }
}
