package com.lightinit.hsdataportal.controller.share;

import com.lightinit.hsdataportal.controller.BaseController;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IDataRequirementService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
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.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author: sunxiang
 * @Date: 2018/7/1 13:41
 * @Description:
 **/
@Controller
@RequestMapping(value = "/share/dataRequirement")
public class DataRequirementShareController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(DataRequirementShareController.class);
    @Autowired
    private IDataRequirementService dataRequirementService;

    /*
    *@Description 需求首页
    *@Param
    *@Date 2018/7/1
    *@return
    **/
    @RequestMapping(value="index.htm")
    public ModelAndView index(DataRequirementSearchModel dataRequirementSearchModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("share/data_requirement/index");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageSize(5);
        pager.setPageNo(pageNo);
        dataRequirementSearchModel.setPage(pager);
        dataRequirementSearchModel.setFlag(0);
        dataRequirementSearchModel.setPlatformType(DicPlatformType.SHARE);
        List<DataRequirementListModel> list=dataRequirementService.getDataRequirementList(dataRequirementSearchModel);
        pager.setTotal(dataRequirementService.getDataRequirementCount(dataRequirementSearchModel));
        modelAndView.addObject("outputModel",list);
        modelAndView.addObject("flag",dataRequirementSearchModel.getFlag());
        modelAndView.addObject("searchName", dataRequirementSearchModel.getRqrmntName());
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    /*
    *@Description 已发布需求详情
    *@Param
    *@Date 2018/7/1
    *@return
    **/
    @RequestMapping(value="detail.htm/{id}")
    public ModelAndView getDataRequirementOpenDetail(@PathVariable("id")  Long id, HttpSession session){
        ModelAndView modelAndView = new ModelAndView("share/data_requirement/detail");
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        Long userId=null;
        if(loginUser!=null){
            userId =loginUser.getId();
        }
        DataRequirementDetailModel outputModol=dataRequirementService.getDataRequirementDetail(id,userId,DicPlatformType.SHARE);
        modelAndView.addObject("outputModel",outputModol);
        return modelAndView;
    }
    /*
    *@Description 发布需求
    *@Param
    *@Date 2018/7/1
    *@return
    **/
    @RequiresAuthentication
    @RequestMapping(value="publish.htm")
    public ModelAndView publish(){
        ModelAndView modelAndView = new ModelAndView("/share/data_requirement/publish");
        List<Map<Long,String>> list=dataRequirementService.getSectorParentNames();
        modelAndView.addObject("sectorNames",list);
        return modelAndView;
    }

    /*
    *@Description 发布
    *@Param
    *@Date 2018/7/1
    *@return
    **/
    @RequiresAuthentication
    @RequestMapping(value="publish.action")
    @ResponseBody
    public ResultState publishDo(@Valid DataRequirementAddModel dataRequirementAddModel, BindingResult bindingResult, HttpSession session){
        if(bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(dataRequirementAddModel, bindingResult);
        }
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        dataRequirementAddModel.setUserId(loginUser.getId());
        dataRequirementAddModel.setPlatformType(DicPlatformType.SHARE);
        dataRequirementAddModel.setRqrmntSectorId(loginUser.getOrgId()) ;
        ResultState resultState= dataRequirementService.addDataRequirement(dataRequirementAddModel);
        return resultState;
    }

    /*
    *@Description 关注
    *@Param 
    *@Date 2018/7/1
    *@return 
    **/
    @RequiresAuthentication
    @RequestMapping(value="follow.action/{id}")
    @ResponseBody
    public ResultState<Long> follow(@PathVariable Long id,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return dataRequirementService.follow(DicPlatformType.SHARE,id,loginUser.getId());
    }

    /*
    *@Description 取消关注
    *@Param 
    *@Date 2018/7/1
    *@return 
    **/
    @RequiresAuthentication
    @RequestMapping(value="delFollow.action/{id}")
    @ResponseBody
    public ResultState<Long> delFollow(@PathVariable Long id,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResultState  resultState = dataRequirementService.delfollow(id);
        return resultState;
    }

        /*
        *@Description 查看更多
        *@Param 
        *@Date 2018/7/2
        *@return 
        **/
       @RequestMapping(value="getMore.action")
       public ModelAndView getMore(DataRequirementSearchModel dataRequirementSearchModel,@RequestParam(defaultValue = "1") int pageNo){
           ModelAndView modelAndView = new ModelAndView("share/data_requirement/more");
           dataRequirementSearchModel.setPlatformType(DicPlatformType.SHARE);
           List<DataRequirementListModel> outputModel=dataRequirementService.getMore(dataRequirementSearchModel,pageNo);
           modelAndView.addObject("outputModel", outputModel);
           return modelAndView;
       }
  /*  @RequestMapping(value="addData.action") 暂时隐藏112
    public ModelAndView addData(){
        ModelAndView modelAndView = new ModelAndView("/share/data_requirement/publish");
        dataRequirementService
    }*/
}
