package com.ylcm.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.ylcm.sys.common.Constants;
import com.ylcm.sys.domain.AdTheatre;
import com.ylcm.sys.domain.AdTheatreTv;
import com.ylcm.sys.service.AdTheatreService;
import com.ylcm.sys.service.AdTheatreTvService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 关联电视剧数据 前端控制器
 * </p>
 *
 * @author liuyu
 * @since 2020-07-01
 */
@Controller
@RequestMapping("/ad-theatre-tv")
public class AdTheatreTvController extends BaseController {

    @Resource
    private AdTheatreService adTheatreService;
    @Resource
    private AdTheatreTvService adTheatreTvService;

    /**
     * to 详情页
     * @param id 剧场数据id
     * @param edit 1-可编辑，0-不可编辑
     */
    @RequestMapping(value = "/detail.do")
    public String detail(Integer id, Integer edit, ModelMap model) {
        // 菜单选中
        model.put("menu", "ad_theatre");
        model.put("parentMenu", "ad_placement");

        model.put("edit", edit);
        AdTheatre adTheatre = adTheatreService.getById(id);
        model.put("adTheatre", adTheatre);
        AdTheatreTv adTheatreTv = adTheatreTvService.getByTheatreId(id);
        model.put("map", adTheatreTv);
        model.put("adTheatreId", id);
        return "adtheatre/tv_detail";
    }

    @RequestMapping(value = "/save.do")
    public void save(AdTheatreTv record, HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();
        result.put("code", Constants.CODE_SUCCESS);
        result.put("msg", "服务器异常");

        String errorMsg = record.toAdTheatreExcelDTO().checkPercent();
        if (StringUtils.isNotBlank(errorMsg)){
            result.put("code", Constants.CODE_FAIL);
            result.put("msg", errorMsg);
            returnResult(request, response, result);
            return;
        }
        if (record.getId() != null) {
            // 更新
            AdTheatreTv adTheatreTv = adTheatreTvService.getByTheatreId(record.getAdTheatreId());
            if (adTheatreTv != null && !adTheatreTv.getId().equals(record.getId())) {
                result.put("code", Constants.CODE_FAIL);
                result.put("msg", "更新失败，请刷新页面后重试！");
                returnResult(request, response, result);
                return;
            }
            adTheatreTvService.add(record);
            result.put("msg", "操作成功");
        } else {
            // 新增
            AdTheatreTv adTheatreTv = adTheatreTvService.getByTheatreId(record.getAdTheatreId());
            if (adTheatreTv != null) {
                result.put("code", Constants.CODE_FAIL);
                result.put("msg", "更新失败，请刷新页面后重试！");
                returnResult(request, response, result);
                return;
            }
            adTheatreTvService.add(record);
            result.put("msg", "操作成功");
        }
        returnResult(request, response, result);
    }
}
