package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.orgclass.VipBindClass;
import com.ssj.bean.sys.orgclass.VipBindClassTemp;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.sys.orgclass.VipBindClassService;
import com.ssj.service.sys.orgclass.VipBindClassTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mount Liu
 * @date 2019/10/22
 */
@Controller
@RequestMapping("/sys/orgclass")
public class VipBindClassController extends BaseController {

    @Autowired
    private VipBindClassService vipBindClassService;

    @Autowired
    private VipBindClassTempService vipBindClassTempService;

    @RequestMapping("/orgclassTempList")
    public String orgclassTempList(Model model,
                                   @RequestParam(required = false, defaultValue = "10") int pageSize,
                                   @RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {

        Map<String, Object> params = new HashMap<>(16);
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI().toString());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<VipBindClassTemp> page = vipBindClassTempService.findByPage(params, initPage(pageNo, pageSize, Direction.DESC, "createTime"));
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list", page.getContent());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);

        return "sys/orgclass/orgclassTempList";
    }

    @RequestMapping("/orgclassTempAdd")
    public String orgclassTempAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        VipBindClassTemp entity = vipBindClassTempService.getById(id);
        if (entity == null) {
            entity = new VipBindClassTemp();
        }
        model.addAttribute("entity", entity);
        return "sys/orgclass/orgclassTempAdd";
    }

    @RequestMapping(value = "/orgclassTempSave", method = RequestMethod.POST)
    @ResponseBody
    public Response orgclassTempSave(VipBindClassTemp entity) {
        logger.info(entity.toString());
        Response response = new Response();
        if (StringUtil.isBlank(entity.getId())) {
            entity.setCreateTime(new Date());
        }
        vipBindClassTempService.save(entity);
        response.success();
        return response;
    }

    @RequestMapping(value = "/orgclassTempDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response orgclassTempDelete(String id) {
        Response response = new Response();
        vipBindClassTempService.delete(id);
        response.success();
        return response;
    }

    /**
     * 导入校验
     *
     * @return
     */
    @RequestMapping(value = "/importCheck", method = RequestMethod.POST)
    @ResponseBody
    public Response importCheck() {
        Response response = new Response();
        vipBindClassTempService.importCheck();
        response.success();
        return response;
    }

    /**
     * 确认导入
     *
     * @return
     */
    @RequestMapping(value = "/comfirmImport", method = RequestMethod.POST)
    @ResponseBody
    public Response comfirmImport() {
        Response response = new Response();
        vipBindClassTempService.comfirmImport();
        response.success();
        return response;
    }

    @RequestMapping("/orgclassList")
    public String orgclassList(Model model,
                               @RequestParam(required = false, defaultValue = "10") int pageSize,
                               @RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {

        Map<String, Object> params = new HashMap<>(16);
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI().toString());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<VipBindClass> page = vipBindClassService.findByPage(params, initPage(pageNo, pageSize, Direction.DESC, "createTime"));
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list", page.getContent());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);

        return "sys/orgclass/orgclassList";
    }

    @RequestMapping("/orgclassAdd")
    public String orgclassAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        VipBindClass entity = vipBindClassService.getById(id);
        if (entity == null) {
            entity = new VipBindClass();
        }
        model.addAttribute("entity", entity);
        return "sys/orgclass/orgclassAdd";
    }

    @RequestMapping(value = "/orgclassSave", method = RequestMethod.POST)
    @ResponseBody
    public Response orgclassSave(VipBindClass entity) {
        logger.info(entity.toString());
        Response response = new Response();
        if (StringUtil.isBlank(entity.getId())) {
            entity.setCreateTime(new Date());
        }
        vipBindClassService.save(entity);
        response.success();
        return response;
    }

    @RequestMapping(value = "/orgclassDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response orgclassDelete(String id) {
        Response response = new Response();
        vipBindClassService.delete(id);
        response.success();
        return response;
    }
}
