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.DirectedFestivalProductEntity;
import com.ibeetl.admin.flow.entity.DirectedProductEntity;
import com.ibeetl.admin.flow.entity.DirectedSmsTemplateEntity;
import com.ibeetl.admin.flow.query.QueryDirectedFestival;
import com.ibeetl.admin.flow.service.DirectedFestivalProductService;
import com.ibeetl.admin.flow.service.DirectedSmsTemplateService;
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.RequestParam;
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 DirectedFestivalProductController {
    private static final String MODEL = "/directed/festivalProduct";
    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
    private DirectedFestivalProductService service;

    @Autowired
    private DirectedSmsTemplateService smsTemplateService;

    @GetMapping(MODEL + "/index.do")
    @Function("festival.index")
    @LogController(content = "活动列表查询", type = "2", description = "")
    public ModelAndView index(HttpServletRequest request,
                              HttpServletResponse response, @RequestParam(name = "fescod") String fescod) {
        ModelAndView mv = new ModelAndView("/directed/festivalproduct/index.html");
        // mv.addObject("search", DirectedFestivalProductEntity.class.getName());
        mv.addObject("fescod", fescod);
        return mv;
    }

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

        service.searchFestivalProduct(page);

        return JsonResult.success(page);
    }

    @PostMapping(MODEL + "/findAllDirectedProduct.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "查询定向产品", type = "2", description = "")
    public List<DirectedProductEntity> findAllDirectedProduct() {
        return service.findAllDirectedProduct();
    }

    @PostMapping(MODEL + "/findAllSmsTemplate.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "查询定向产品", type = "2", description = "")
    public List<DirectedSmsTemplateEntity> findAllSmsTemplate(String cpid) {
        return smsTemplateService.findSmstemplateByCpid(cpid);
    }

    @PostMapping(MODEL + "/findAllCpinfo.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "查询所有的合作方", type = "2", description = "")
    public List<DirectedSmsTemplateEntity> findAllCpinfo() {
        return service.findAllCpinfo();
    }

    @GetMapping(MODEL + "/add.do")
    @Function("festival.index")
    @LogController(content="关联活动产品",type="2", description="")
    public ModelAndView add(@RequestParam(name = "fescod") String fescod) {
        ModelAndView view = new ModelAndView("/directed/festivalproduct/add.html");
        view.addObject("fescod", fescod);
        DirectedFestivalProductEntity entity = new DirectedFestivalProductEntity();
        entity.setFescod(fescod);
        view.addObject("entity", entity);
        return view;
    }

    @PostMapping(MODEL + "/add.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "关联活动产品", type = "2", description = "")
    public JsonResult add(DirectedFestivalProductEntity entity) {

        String sms = entity.getOrdervercode() + "," + entity.getCancelvercode() + "," +
                entity.getPrevercode() + "," + entity.getActivevercode() + "," +
                entity.getDrawvercode() + "," + entity.getOrderfail();

        entity.setIcon(sms);
//        entity.setSmstype(entity.getOrdervercode());
        entity.setSmstype("800");

        service.saveFestivalProduct(entity);
        return JsonResult.success();
    }

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

        service.deleteFestivalProductById(entity);

        return JsonResult.success();
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("festival.index")
    @LogController(content="关联活动产品",type="2", description="")
    public ModelAndView edit(@RequestParam(name = "id") String id) {
        ModelAndView view = new ModelAndView("/directed/festivalproduct/edit.html");
        DirectedFestivalProductEntity entity = service.findById(id);
        view.addObject("entity", entity);

        return view;
    }

    @PostMapping(MODEL + "/edit.json")
    @Function("festival.index")
    @ResponseBody
    @LogController(content = "关联活动产品", type = "2", description = "")
    public JsonResult edit(DirectedFestivalProductEntity entity) {

        String sms = entity.getOrdervercode() + "," + entity.getCancelvercode() + "," +
                entity.getPrevercode() + "," + entity.getActivevercode() + "," +
                entity.getDrawvercode() + "," + entity.getOrderfail();

        entity.setIcon(sms);
        entity.setSmstype("800");
//        entity.setSmstype(entity.getOrdervercode());

        service.updateFestivalProduct(entity);
        return JsonResult.success();
    }
}
