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.LoginUser;
import com.lightinit.hsdataportal.model.MyRequirementListModel;
import com.lightinit.hsdataportal.model.MyRequirementSearchModel;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.service.IMyDataRequirementService;
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/MyOpenDataRequirement")
public class MyDataRequirementOpenController {
    @Autowired
    private IMyDataRequirementService myDataRequirementService;

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

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


}
