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.DirectedChannelEntity;
import com.ibeetl.admin.flow.query.QueryDirectedChannel;
import com.ibeetl.admin.flow.service.DirectedChannelService;
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 DirectedChannelController {

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/directed/channel";

    @Autowired
    private DirectedChannelService directedChannelService;

    @GetMapping(MODEL + "/index.do")
    @Function("directedChannel.index")
    @LogController(content = "前向渠道管理", type = "2", description = "")
    public ModelAndView index(HttpServletRequest request,
                              HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("/directed/channel/index.html");
        mv.addObject("search", DirectedChannelEntity.class.getName());
        return mv;
    }

    @PostMapping(MODEL + "/list.json")
    @Function("directedChannel.index")
    @ResponseBody
    @LogController(content = "前向渠道管理", type = "2", description = "")
    public JsonResult<PageQuery<DirectedChannelEntity>> index(QueryDirectedChannel condition) {
        PageQuery<DirectedChannelEntity> page = condition.getPageQuery();

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

    @PostMapping(MODEL + "/findAllChannel.json")
    @Function("directedChannel.index")
    @ResponseBody
    @LogController(content = "前向渠道管理", type = "2", description = "")
    public List<DirectedChannelEntity> findAllChannel() {

        return directedChannelService.findAllChannel();
    }

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

    @PostMapping(MODEL + "/add.json")
    @Function("directedChannel.index")
    @ResponseBody
    @LogController(content = "添加渠道", type = "2", description = "")
    public JsonResult add(DirectedChannelEntity entity) {

        directedChannelService.saveChannel(entity);

        return JsonResult.success();
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("directedChannel.index")
    @LogController(content = "修改渠道", type = "2", description = "")
    public ModelAndView edit(String id) {
        ModelAndView view = new ModelAndView("/directed/channel/edit.html");
        view.addObject("entity", directedChannelService.findChannelById(id));
        return view;
    }

    @PostMapping(MODEL + "/edit.json")
    @Function("directedChannel.index")
    @ResponseBody
    @LogController(content = "修改渠道", type = "2", description = "")
    public JsonResult edit(DirectedChannelEntity entity) {

        directedChannelService.updateChannel(entity);

        return JsonResult.success();
    }
}
