package com.lightinit.hsdataportal.controller.share;

import com.lightinit.hsdataportal.controller.BaseController;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicResourceType;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IMicroappService;
import com.lightinit.hsdataportal.service.IMyMicroappService;
import com.lightinit.hsdataportal.service.IResourceFollowService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
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.servlet.view.InternalResourceViewResolver;

import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping(value = "/share/myMicroapp")
public class myShareMicroappController extends BaseController {
    @Autowired
    private IMyMicroappService myMicroappService;
    @Autowired
    private IResourceFollowService followService ;
    @RequiresAuthentication
    @RequestMapping(value = "shareList.action")
    public String applyList(@RequestParam(defaultValue = "0")Long falg, Long msg , MyMicroappApplySearchModel inputModel, HttpSession session, @RequestParam(defaultValue = "1") int pageNo, Model model) {
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setApplyBy(loginUser.getId());
        inputModel.setPage(pager);
        inputModel.setResourceType(DicResourceType.MICROAPP);
        inputModel.setPlatformType(DicPlatformType.SHARE);
        List<MyMicroappApplyListModel> applyListModels = myMicroappService.selectOpenList(inputModel);
        model.addAttribute("applyList", applyListModels);
        MyMicroappFollowSearchModel followSearchModel = new MyMicroappFollowSearchModel() ;
        BeanUtils.copyProperties(inputModel,followSearchModel);
        followSearchModel.setFollowId(loginUser.getId());
        List<MyMicroappFollowListModel> followListModels = myMicroappService.selectFollowOpenList(followSearchModel);
        model.addAttribute("followList", followListModels);
        model.addAttribute("falg", falg);
        model.addAttribute("msg", msg);
        return "share/myMicroapp/list";
    }

    @RequestMapping(value = "noFollow.action/{id}")
    public String view(@PathVariable Long id){
        ResultState resultState  =  followService.noFollow(id);
        if (StringUtils.isEmpty(resultState.getMsg())){
            return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/share/myMicroapp/shareList.action?falg=1&msg="+resultState.getMsg()+"#shujv" ;
        }
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/share/myMicroapp/shareList.action?falg=1#shujv" ;

    }
}
