package com.ibeetl.admin.flow.controller;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.annotation.LogController;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.admin.flow.entity.DirectedFestivalEntity;
import com.ibeetl.admin.flow.query.QueryDirectedFestival;
import com.ibeetl.admin.flow.service.DirectedFestivalService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 前向活动管理
 *
 * @author Tao
 */
@Controller
public class DirectedFestivalController {
    private static final String MODEL = "/directed/festival";
    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
    private DirectedFestivalService directedFestivalService;

    @GetMapping(MODEL + "/index.do")
    @Function("festival.index")
    @LogController(content = "活动列表查询", type = "2", description = "")
    // 用户免费资源查询
    public ModelAndView index(HttpServletRequest request,
                              HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("/directed/festival/index.html");
        mv.addObject("search", DirectedFestivalEntity.class.getName());
        return mv;
    }

    @PostMapping(MODEL + "/list.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "活动列表查询", type = "2", description = "")
    public JsonResult<PageQuery<DirectedFestivalEntity>> index(QueryDirectedFestival condition) {
        PageQuery<DirectedFestivalEntity> page = condition.getPageQuery();

        directedFestivalService.search(page);
        return JsonResult.success(page);
    }

    @PostMapping(MODEL + "/findAllFestival.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "活动列表查询", type = "2", description = "")
    public List<DirectedFestivalEntity> findAllFestival() {

        return directedFestivalService.findAllFestival();
    }

    @GetMapping(MODEL + "/add.do")
    @Function("festival.index")
    @LogController(content="添加活动",type="2",description="")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/directed/festival/add.html");
        return view;
    }

    @PostMapping(MODEL + "/add.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "修改活动", type = "2", description = "")
    public JsonResult add(DirectedFestivalEntity entity) {

        directedFestivalService.saveFestival(entity);

        return JsonResult.success();
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("festival.index")
    @LogController(content="修改活动",type="2",description="")
    public ModelAndView edit(String id) {
        ModelAndView view = new ModelAndView("/directed/festival/edit.html");
        view.addObject("entity", directedFestivalService.findById(id));
        return view;
    }

    @PostMapping(MODEL + "/edit.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "修改URL添加", type = "2", description = "")
    public JsonResult edit(DirectedFestivalEntity entity) {

        directedFestivalService.updateFestival(entity);

        return JsonResult.success();
    }

    @PostMapping(MODEL + "/del.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "删除URL添加", type = "2", description = "")
    public JsonResult del(DirectedFestivalEntity entity) {
        try {
            directedFestivalService.deleteFesAndChannel(entity);
        } catch (Exception e) {
            return JsonResult.failMessage(e.getMessage());
        }

        return JsonResult.success();
    }
}
