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.IMyMicroappRequirementService;
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/MyMicroappRequirement")
public class MyMicroappRequirementController {
    @Autowired
    private IMyMicroappRequirementService myMicrroappRequirementService;

    @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/mymicroapp/list");
        List<MyRequirementListModel> outputModel = myMicrroappRequirementService.selectOpenList(loginUser.getId());
        modelAndView.addObject("list", outputModel);
        return modelAndView;
    }

    @RequestMapping(value = "edit.action")
    @ResponseBody
    @RequiresAuthentication
    public ResultState<Long> edit(RequirementMicroappPublishModel inputModel){
        return myMicrroappRequirementService.edit(inputModel);
    }

    /**
     * 下架
     * @param id
     * @return
     */
    @RequestMapping(value = "offLine.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState<Long> offLine(@PathVariable Long id){
        Requirement requirement = myMicrroappRequirementService.selectRequirementById(id);
        requirement.setRqrmntState(DicRequirmentAuditState.OFFSHELVES);
        return myMicrroappRequirementService.offLine(requirement);
    }


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


}
