package com.lightinit.hsdataportal.controller.open;

import com.lightinit.hsdataportal.dictionary.DicRequirmentAuditState;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.entity.Requirement;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IAPIDemandService;
import com.lightinit.hsdataportal.service.IMyApiRequirementService;
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.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/open/MyApiRequirement")
public class MyApiRequirementController {
    @Autowired
    private IMyApiRequirementService myApiRequirementService;
    @Autowired
    private IAPIDemandService demandService ;

    @RequestMapping(value = "list.action")
    @ResponseBody
    @RequiresAuthentication
    public ModelAndView list(HttpSession session) {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView = new ModelAndView("open/myrequirement/myapi/list");
        List<MyRequirementListModel> outputModel = myApiRequirementService.selectOpenList(loginUser.getId());
        modelAndView.addObject("list", outputModel);
        return modelAndView;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "view.action/{id}")
    @RequiresAuthentication
    public ModelAndView openView(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("open/apiDemand/view");
        APIDemandOpenViewModel demandOpenViewModel = demandService.queryView(id);
        modelAndView.addObject("demandOpenViewModel", demandOpenViewModel);
        return modelAndView;
    }

    @RequestMapping(value = "edit.action")
    @ResponseBody
    @RequiresAuthentication
    public ResultState<Long> edit(APIDdmandOpenReleaseModel releaseModel){
        return myApiRequirementService.edit(releaseModel);
    }
    /**
     * 我的接单列表
     * @return
     */
    @RequestMapping(value = "receiveList.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ModelAndView receiveList(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("open/myrequirement/myapi/receiveList");
        List<APIReceiveListModel> outputModel = myApiRequirementService.selectReceiveList(id);
        modelAndView.addObject("list", outputModel);

        return modelAndView;
    }
    @RequestMapping(value = "delfollow.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState<Long> offLine(@PathVariable Long id){
        Requirement requirement = myApiRequirementService.selectRequirementById(id);
        requirement.setRqrmntState(DicRequirmentAuditState.OFFSHELVES);
        return myApiRequirementService.offLine(requirement);
    }
}
