package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicRoadShowAudit;
import com.lightinit.hsdataplatform.dictionary.DicRoadShowStage;
import com.lightinit.hsdataplatform.dictionary.DicRoadShowState;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.impl.AdminServiceImpl;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.LoginUser;
import com.lightinit.hsdataplatform.model.admin.RoadShowSearchModel;
import com.lightinit.hsdataplatform.service.admin.IRoadShowAuditService;
import com.lightinit.hsdataplatform.service.admin.IRoadShowMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/roadshow_audit")
public class RoadShowAuditController {
    @Autowired
    private IRoadShowAuditService roadShowAuditService;
    @Autowired
    private IRoadShowMgrService roadShowMgrService;
    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/roadshow_audit/index");
        modelAndView.addObject("dicRoadShowStage", DicRoadShowStage.getInstacne().getDicMap());
        modelAndView.addObject("dicRoadShowAudit", DicRoadShowAudit.getInstacne().getDicMap());
        return modelAndView;
    }

    /**
     * 查询
     * @param inputModel
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "list.action")
    public ModelAndView list(RoadShowSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("admin/roadshow_audit/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<Roadshow> outputModel = roadShowAuditService.queryList(inputModel, pager);
        pager.setTotal(roadShowAuditService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
       // modelAndView.addObject("dicRoadShowAudit", DicRoadShowAudit.getInstacne().getDicMap());
        return modelAndView;
    }


    /**
     * 查看某一个路演详情
     * @param session
     * @param id
     * @return
     */
    @RequestMapping(value = "detail.htm/{id}")
    public ModelAndView detail(HttpSession session,@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/roadshow_audit/detail");
        Map<String, Object> map= roadShowMgrService.queryOne(id);
        modelAndView.addObject("busiModel",map.get("roadshowWithBLOBs"));
        modelAndView.addObject("auditor",map.get("auditor"));
        return modelAndView;
    }
    /**
     * 审核路演
     * @param
     * @param id
     * @return
     */
    @RequestMapping(value = "audit.htm/{id}")
    public ModelAndView audit(@PathVariable long id,HttpSession session){
        ModelAndView modelAndView = new ModelAndView("admin/roadshow_audit/audit");
        Map<String, Object> map = roadShowMgrService.queryOne(id);
        LoginUser loginUser = (LoginUser)session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        modelAndView.addObject("busiModel",map.get("roadshowWithBLOBs"));
        modelAndView.addObject("auditor",loginUser.getUsername());
        modelAndView.addObject("auditerId",loginUser.getId());
        return modelAndView;
    }

    /**
     * 审核路演
     * @param inputModel
     * @return
     */
    @RequestMapping(value="edit.action")
    @ResponseBody
    public ResultState doEdit(HttpSession session,RoadshowWithBLOBs inputModel,int param){
        //将登录的用户信息从session中取出，并设置id
        LoginUser loginUser = (LoginUser)session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setAuditedBy(loginUser.getId());
        if(param==1){
            inputModel.setRoadshowState(DicRoadShowAudit.APPROVED);
            inputModel.setAuditResult(DicRoadShowState.APPROVED);
            return roadShowAuditService.doEdit(inputModel);
        }
        if(param==2){
            inputModel.setRoadshowState(DicRoadShowAudit.APPROVED);
            inputModel.setAuditResult(DicRoadShowState.REJECTED);
            return roadShowAuditService.doEdit(inputModel);
        }
        return null;
    }
}
