package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.common.ConfigUtils;
import com.lightinit.hsdataplatform.common.SysContants;
import com.lightinit.hsdataplatform.dictionary.DicRoadShowAudit;
import com.lightinit.hsdataplatform.dictionary.DicRoadShowStage;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.Roadshow;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.IDataAuditService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.protocol.HTTP;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/6/5 17:07
 * @Description:
 **/
@Controller
@RequestMapping("/admin/data_audit")
public class DataAuditController {

    @Autowired
    private IDataAuditService dataAuditService;
    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/data_audit/index");
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(DataPublishSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setUserId(loginUser.getId());
        ModelAndView modelAndView = new ModelAndView("admin/data_audit/list");
        BaseExample.Page pager = new BaseExample.Page(pageNo,10);
        inputModel.setPage(pager);
        List<DataPublishListModel> outputModel = dataAuditService.queryList(inputModel);
        pager.setTotal(dataAuditService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    @RequestMapping(value="detail.htm/{id}")
    public ModelAndView publishResourceDetail(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("forward:/pages/admin/data_audit/detail.jsp");
        DataPublishDetailModel dataPublishDetailModel = dataAuditService.queryOne(id);
        //获取文件名
        dataPublishDetailModel.setDataFile(StringUtils.substringAfterLast(dataPublishDetailModel.getDataFile(),"\\"));
        dataPublishDetailModel.setDemoFile(StringUtils.substringAfterLast(dataPublishDetailModel.getDemoFile(),"\\"));
        //获取图片映射路径
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        dataPublishDetailModel.setResourceImg(imageContextPath+dataPublishDetailModel.getResourceImg());
        modelAndView.addObject("busiModel",dataPublishDetailModel);
        return modelAndView;
    }

    @RequestMapping(value="audit.htm/{id}")
    public ModelAndView publishResourceAudit(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("forward:/pages/admin/data_audit/audit.jsp");
        DataPublishDetailModel dataPublishDetailModel = dataAuditService.queryOne(id);
        //获取文件名
        dataPublishDetailModel.setDataFile(StringUtils.substringAfterLast(dataPublishDetailModel.getDataFile(),"\\"));
        dataPublishDetailModel.setDemoFile(StringUtils.substringAfterLast(dataPublishDetailModel.getDemoFile(),"\\"));
        //获取图片映射路径
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        dataPublishDetailModel.setResourceImg(imageContextPath+dataPublishDetailModel.getResourceImg());
        modelAndView.addObject("busiModel",dataPublishDetailModel);
        return modelAndView;
    }

    /*
    *@Description
    *@Param     param :1:审核通过，2：审核不通过
    *@Date 2018/6/26
    *@return
    **/
    @RequiresAuthentication
    @RequestMapping(value="audit.action")
    @ResponseBody
    public ResultState<Long> audit(Long id, String auditComment, int param, HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResultState<Long> resultState = dataAuditService.audit(id,auditComment,param,loginUser.getId());
        return resultState;
    }
}
