package cn.pluss.platform.controller.level;


import java.util.Map;
import java.util.List;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.util.StringUtil;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.pluss.platform.entity.Level;
import cn.pluss.platform.level.LevelService;

@Controller
@RequestMapping("level")
public class LevelController extends BaseModelController<LevelService, Level> {

    private Integer PAGE_SIZE = 12;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Integer pageIndex) {

        return "level/levelList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryLevelByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryLevelByPage(Integer currPage, Integer id) {

        if (currPage == null) {
            currPage = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        map.put("id", id);
        List<Level> levelList = baseService.queryLevelPage(map);
        Integer count = baseService.queryLevelPageCount(map);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("levelList", levelList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryLevelDetails", method = RequestMethod.GET)
    public String queryLevelDetails(HttpServletRequest request, Long id, Long pageIndex) {

        if (id != null && id != 0) {
            Level level = new Level();
            level.setId(id);
            level = baseService.queryLevel(level);
            request.setAttribute("level", level);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "level/levelDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryLevelBaseInfo", method = RequestMethod.GET)
    public String queryLevelBaseInfo(HttpServletRequest request, Long id, Long pageIndex) {

        if (id != null && id != 0) {
            Level level = new Level();
            level.setId(id);
            level = baseService.queryLevel(level);
            BigDecimal rate = BigDecimal.valueOf(level.getRate());

            request.setAttribute("rate", rate);
            request.setAttribute("level", level);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "level/levelBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryLevelExtendsInfo", method = RequestMethod.GET)
    public String queryLevelExtendsInfo() {

        return "level/levelExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryLevelFunctionsInfo", method = RequestMethod.GET)
    public String queryLevelFunctionsInfo() {

        return "level/levelFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws ParseException
     */
    @RequestMapping(value = "/saveLevel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveLevel(Level level) throws ParseException {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        String createDtSTR = level.getCreateDtSTR();
        if (!StringUtil.isEmpty(createDtSTR)) {
            Date date2 = date.parse(createDtSTR);
            level.setCreateDt(date2);
        }
        if (level.getId() != null && level.getId() != 0) {
            baseService.updateLevel(level);
        } else {
            baseService.saveLevel(level);
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 修改对象
     *
     */
    @RequestMapping(value = "/updateLevel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateLevel(Long id) {

        Level level = new Level();
        level.setId(id);
        baseService.updateLevel(level);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

}