package com.lightinit.hsdataportal.controller.open;

import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicRequirmentAuditState;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.entity.RequirementFollow;
import com.lightinit.hsdataportal.entity.ResourceCatelog;
import com.lightinit.hsdataportal.entity.ResourceFollow;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IRequirementMicroappService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.lightinit.hsdataportal.controller.BaseController.getAllFieldInvalidResultState;

@Controller
@RequestMapping(value = "/open/requirementMicroapp")
public class RequirementMicroappOpenController {
    @Autowired
    private IRequirementMicroappService requirementMicroappService;
    @RequestMapping(value = "openList.action")
    @ResponseBody
    public ModelAndView openList(RequirementMicroappSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("/open/requirement_microapp/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setRqrmntState(DicRequirmentAuditState.ONPROSCESSING);
        List<RequirementMicroappListModel> outputModel = requirementMicroappService.selectOpenList(inputModel, pager);
        pager.setTotal(requirementMicroappService.selectOpenCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        modelAndView.addObject("flag", inputModel.getFlag());
        modelAndView.addObject("newTime", new Date());
        return modelAndView;
    }

    /**
     * 详情页面
     * @param id
     * @return
     */
    @RequestMapping(value = "view.htm/{id}")
    public ModelAndView view(@PathVariable long id) {
        ModelAndView modelAndView = new ModelAndView("/open/requirement_microapp/detail");
        RequirementMicroappViewModel outPutModel = requirementMicroappService.queryView(id);
        modelAndView.addObject("requirement", outPutModel);
        return modelAndView;
    }
    @RequestMapping(value = "toPublish.htm")
    @RequiresAuthentication
    public ModelAndView toPublish() {
        ModelAndView modelAndView = new ModelAndView("/open/requirement_microapp/publish");
        List<ResourceCatelog> obj = requirementMicroappService.selectCatelog();
        modelAndView.addObject("obj", obj);
        return modelAndView;
    }


    @RequestMapping(value = "publish.action")
    @RequiresAuthentication
    @ResponseBody
    public ResultState publish(HttpSession session, @Valid RequirementMicroappPublishModel inputModel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setPublishBy(loginUser.getId());

        return requirementMicroappService.publish(inputModel);
    }

    /**
     *关注
     * setting表id
     * @param session
     * @param id
     * @return
     */
    @RequestMapping(value = "follow.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState follow(HttpSession session,@PathVariable long id) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        RequirementFollow model = new RequirementFollow();
        model.setFollowerId(loginUser.getId());//关注人id
        model.setRqrmntId(id);//资源发布id
        model.setPlatformType(DicPlatformType.OPEN);
        model.setCreatedAt(new Date());

        ResultState   resultState = requirementMicroappService.follow(model);
        return resultState;
    }

    /**
     * 接单
     * @param inputModel
     * @return
     */
    @RequestMapping(value = "receive.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState receive(HttpSession session,RequirementReceiveModel inputModel,@PathVariable Long id) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setRqrmntId(id);
        inputModel.setReceivedBy(loginUser.getId());
        ResultState   resultState = requirementMicroappService.receive(inputModel);

        return resultState;
    }

}
