package com.lightinit.hsdataportal.controller.share;

//import com.lightinit.hsdataportal.dictionary.DicMicroappRequirmentAuditState;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicRequirmentAuditState;
import com.lightinit.hsdataportal.dictionary.DicShareDataRequirementState;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.entity.DataRequirementData;
import com.lightinit.hsdataportal.entity.Requirement;
import com.lightinit.hsdataportal.entity.RequirementData;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IDataRequirementService;
import com.lightinit.hsdataportal.service.IMyDataRequirementService;
import com.lightinit.hsdataportal.service.IMyFollowRequirementService;
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.util.List;
import java.util.Map;

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

@Controller
@RequestMapping(value = "/share/MyShareDataRequirement")
public class MyDataRequirementShareController {
    @Autowired
    private IMyDataRequirementService myDataRequirementService;
    @Autowired
    private IMyFollowRequirementService myFollowRequirementService;
    @Autowired
    private IDataRequirementService dataRequirementService;
    @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("share/mydatarequirement/index");
        List<MyRequirementListModel> dataList = myDataRequirementService.selectShareList(loginUser.getId());
        List<MyFollowRequirementListModel> followList = myFollowRequirementService.selectShareList(loginUser.getId());
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("followList", followList);
        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(DicShareDataRequirementState.OFFSHELVES);
        return myDataRequirementService.offLine(requirement);
    }
    @RequestMapping(value = "del.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState<Long> del(@PathVariable Long id){
        return myDataRequirementService.del(id);
    }
    @RequestMapping(value = "publish.action/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ResultState<Long> publish(@PathVariable Long id){
        Requirement requirement = myDataRequirementService.selectRequirementById(id);
        requirement.setRqrmntState(DicShareDataRequirementState.PENDING);
        return myDataRequirementService.del(id);
    }

    @RequestMapping(value = "edit.htm/{id}")
    @ResponseBody
    @RequiresAuthentication
    public ModelAndView edit(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("share/mydatarequirement/edit");
        Requirement requirement = myDataRequirementService.selectById(id);
        RequirementData requirementData = myDataRequirementService.selectByRqmentId(id);
        List<DataRequirementData> dataRequirementData = myDataRequirementService.selectByDataRqmentId(id);
        List<Map<Long,String>> list=dataRequirementService.getSectorParentNames();
        modelAndView.addObject("sectorNames",list);
        modelAndView.addObject("requirement", requirement);
        modelAndView.addObject("requirementData", requirementData);
        modelAndView.addObject("dataRequirementData", dataRequirementData);
        return modelAndView;
    }
    @RequestMapping(value = "doEdit.action")
    @ResponseBody
    @RequiresAuthentication
    public ResultState doEdit(@Valid DataRequirementAddModel inputModel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        ResultState resultState= myDataRequirementService.doEdit(inputModel);
        return resultState;
    }




}
