package com.dgtly.sync.controller;

import java.util.List;

import com.dgtly.sync.domain.SyncFailDetail;
import com.dgtly.sync.service.IAnalysisSysUserService;
import com.dgtly.sync.service.ISyncFailDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dgtly.sync.domain.SyncLog;
import com.dgtly.sync.service.ISyncLogService;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.common.core.page.TableDataInfo;

/**
 * 同步解析日志Controller
 *
 * @author qxp
 * @date 2020-07-14
 */
@Controller
@RequestMapping("/sync/synclog")
public class SyncLogController extends BaseController
{
    private String prefix = "sync/synclog";

    @Autowired
    private ISyncLogService syncLogService;
    @Autowired
    private IAnalysisSysUserService analysisSysUserService;

    @Autowired
    private ISyncFailDetailService syncFailDetailService;

    @RequiresPermissions("sync:synclog:view")
    @GetMapping()
    public String synclog()
    {
        return prefix + "/synclog";
    }

    /**
     * 查询同步解析日志列表
     */
    @RequiresPermissions("sync:synclog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SyncLog syncLog)
    {
        startPage();
        List<SyncLog> list = syncLogService.selectSyncLogList(syncLog);
        return getDataTable(list);
    }

    /**
     * 导出同步解析日志列表
     */
    @RequiresPermissions("sync:synclog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SyncLog syncLog)
    {
        List<SyncLog> list = syncLogService.selectSyncLogList(syncLog);
        ExcelUtil<SyncLog> util = new ExcelUtil<SyncLog>(SyncLog.class);
        return util.exportExcel(list, "synclog");
    }

    /**
     * 同步解析日志详情
     */
    @GetMapping("/faildetail/{packageId}")
    public String faildetail(@PathVariable("packageId") String packageId, ModelMap mmap)
    {
        mmap.put("packageId", packageId);
        List<String> exceptionTypeList = syncFailDetailService.selectSyncExceptionTypeList(packageId);
        mmap.put("exceptionTypeList",exceptionTypeList);
        return prefix + "/syncfaildetail";
    }


    /**
     * 查询同步解析失败详情列表
     */
    @PostMapping("/faildetaillist")
    @ResponseBody
    public TableDataInfo list(SyncFailDetail syncFailDetail)
    {
        startPage();
        List<SyncFailDetail> list = syncFailDetailService.selectSyncFailDetailList(syncFailDetail);
        return getDataTable(list);
    }


    /**
     * 解析用户数据
     * @return
     */
    @RequiresPermissions("sync:synclog:resyncuser")
    @GetMapping("reSyncUser")
    @ResponseBody
    public Object user(){
        try{
            analysisSysUserService.analysisSysUser(null);
            //analysisSysUserService.analysisSysCwUser(null);
            analysisSysUserService.analysisSysYSLUser(null);
            analysisSysUserService.analysisSysDivisionUser();
            analysisSysUserService.analysisSysApplicationServiceUser();
            analysisSysUserService.analysisSysClueReporting();
        }catch (Exception e){
            logger.error("解析用户出错",e);
        }
        return AjaxResult.success();
    }


}
