package cn.pluss.platform.controller;

import cn.hutool.core.comparator.CompareUtil;
import cn.pluss.platform.AppMenuService;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.AppMenu;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author DJH
 */
@RequestMapping("appMenu")
@RestController
public class AppMenuController extends BaseModelController<AppMenuService, AppMenu> {

    @GetMapping("homePageMenu")
    public Result<List<Map<String, Object>>> getHomePageMenu(HttpServletRequest request) {
        List<Map<String, Object>> homeMenu = baseService.getHomeMenu(request);
        return ResultGenerator.genSuccessResult(homeMenu);
    }

    @GetMapping("spreadPageMenu")
    public Result<List<Map<String, Object>>> getPromoterPageMenu(HttpServletRequest request) {
        List<Map<String, Object>> homeMenu = baseService.getSpreadPageMenu(request);
        return ResultGenerator.genSuccessResult(homeMenu);
    }

    @GetMapping("menuIsUpdated")
    public Result<?> getInfoByName(String name, String cacheTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd ahh:mm:ss");

        Date mCacheTime = null;
        if (StringUtils.isNotEmpty(cacheTime) ) {
            if (((cacheTime.contains("上午")) || (cacheTime.contains("下午")))) {
                mCacheTime = sdf2.parse(cacheTime);
            } else {
                mCacheTime = sdf.parse(cacheTime);
            }
        }

        LambdaQueryWrapper<AppMenu> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(AppMenu::getName, name);
        final AppMenu one = baseService.getOne(qWrapper);

        return ResultGenerator.genSuccessResult(CompareUtil.compare(one.getUpdateTime(), mCacheTime) > 0);
    }
}
