package com.ygtx.system.service.impl;

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

import com.ygtx.system.domain.SysPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.system.domain.SysOperLog;
import com.ygtx.system.mapper.SysOperLogMapper;
import com.ygtx.system.service.ISysOperLogService;

/**
 * 操作日志 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl implements ISysOperLogService
{
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     * 
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog)
    {
        operLogMapper.insertOperlog(operLog);
    }

    /**
     * 查询系统操作日志集合
     * 
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog)
    {
        return operLogMapper.selectOperLogList(operLog);
    }

    /**
     * 批量删除系统操作日志
     * 
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    public void createDefaultForm(List<SysOperLog> zr,List<SysOperLog> zz,List<SysOperLog> yg) {
        // wbzr 列表转换
        SysOperLog log1 = new SysOperLog();
        log1.setTitle("工单管理");
        log1.setIcon("fa fa-tasks");
        log1.setColor("blue");

        SysOperLog log2 = new SysOperLog();
        log2.setTitle("班组管理");
        log2.setIcon("fa fa-users");
        log2.setColor("cyan");

        SysOperLog log3 = new SysOperLog();
        log3.setTitle("工分管理");
        log3.setIcon("fa fa-star");
        log3.setColor("orange");

        SysOperLog log4 = new SysOperLog();
        log4.setTitle("工时分析");
        log4.setIcon("fa fa-clock-o");
        log4.setColor("gray");

        SysOperLog log5 = new SysOperLog();
        log5.setTitle("通知中心");
        log5.setIcon("fa fa-bell");
        log5.setColor("red");
        zr.add(log1);
        zr.add(log2);
        zr.add(log3);
        zr.add(log4);
        zr.add(log5);

// zz 列表转换
        SysOperLog log11 = new SysOperLog();
        log11.setTitle("工单分配");
        log11.setIcon("fa fa-tasks");
        log11.setColor("blue");

        SysOperLog log12 = new SysOperLog();
        log12.setTitle("组员管理");
        log12.setIcon("fa fa-user");
        log12.setColor("cyan");

        SysOperLog log13 = new SysOperLog();
        log13.setTitle("工分管理");
        log13.setIcon("fa fa-star");
        log13.setColor("orange");

        SysOperLog log14 = new SysOperLog();
        log14.setTitle("超时工单");
        log14.setIcon("fa fa-exclamation-circle");
        log14.setColor("red");
        zz.add(log11);
        zz.add(log12);
        zz.add(log13);
        zz.add(log14);

// yg 列表转换
        SysOperLog log21 = new SysOperLog();
        log21.setTitle("我的工单");
        log21.setIcon("fa fa-tasks");
        log21.setColor("blue");

        SysOperLog log22 = new SysOperLog();
        log22.setTitle("我的工分");
        log22.setIcon("fa fa-star");
        log22.setColor("orange");

        SysOperLog log23 = new SysOperLog();
        log23.setTitle("扣分工单");
        log23.setIcon("fa fa-minus-circle");
        log23.setColor("red");

        SysOperLog log24 = new SysOperLog();
        log24.setTitle("历史记录");
        log24.setIcon("fa fa-history");
        log24.setColor("gray");
        yg.add(log21);
        yg.add(log22);
        yg.add(log23);
        yg.add(log24);
    }

    /**
     * 统计用户操作次数
     * 
     * @param operName 操作人员名称
     * @return 结果
     */
    @Override
    public List<SysOperLog> selectOperLogCountByOperName(String operName, int postType)
    {
        List<SysOperLog> wbzr = new ArrayList<>();
        List<SysOperLog> zz = new ArrayList<>();
        List<SysOperLog> yg = new ArrayList<>();
        createDefaultForm(wbzr,zz,yg);
        List<SysOperLog> result = operLogMapper.selectOperLogCountByOperName(operName);
        if (!result.isEmpty()) {
            for (int i=0;i<result.size();i++) {
                SysOperLog map = result.get(i);
                map.setColor(wbzr.get(i).getColor());
            }
        }
        if (result.size()<5) {
            List<SysOperLog> highUse = new ArrayList<>();
            if (postType==2) {
                highUse.addAll(wbzr);
            } else if (postType==1) {
                highUse.addAll(zz);
            } else {
                highUse.addAll(yg);
            }
            if (result.isEmpty()) {
                result.addAll(highUse);
            } else {
                for (SysOperLog map : highUse) {
                    if (result.size()>3) {
                        break;
                    }
                    boolean isadd = true;
                    for (SysOperLog map1 : result) {
                        if (map1.getTitle().equals(map.getTitle())) {
                            isadd = false;
                            break;
                        }
                    }
                    if (isadd) {
                        result.add(map);
                    }
                }
            }
        }
        return result;
    }

    private static Map<String, String> createMenu(String title, String icon, String color) {
        Map<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("icon", icon);
        map.put("color", color);
        return map;
    }

    public Map<String, Object> createDataMap(String key, String value) {
    Map<String, Object> map = new HashMap<>();
    map.put("key", key);
    map.put("value", value);
    return map;
}

    /**
     * 查询操作日志详细
     * 
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog();
    }
}
