package cn.pluss.platform.controller.sys;

import cn.pluss.platform.AppMenuService;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.appMenuUserType.AppMenuUserTypeService;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.AppMenu;
import cn.pluss.platform.entity.AppMenuUserType;
import cn.pluss.platform.entity.Dict;
import cn.pluss.platform.util.OssOperatUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("appMenu")
public class AppMenuController extends BaseModelController<AppMenuService, AppMenu> {

    @Autowired
    private DictService dictService;

    @Autowired
    private AppMenuUserTypeService appMenuUserTypeService;

    @Autowired
    private AppMenuService appMenuService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        List<Dict> menuGroup = dictService.getByCode(Collections.singletonList("APP_MENU_GROUP"));
        List<Dict> navCode = dictService.getByCode(Collections.singletonList("APP_MENU_NAV"));

        request.setAttribute("menuGroup", menuGroup);
        request.setAttribute("navCode", navCode);

        request.setAttribute("menuGroupString", JSON.toJSONString(menuGroup));
        request.setAttribute("navCodeString", JSON.toJSONString(navCode));

        return "sys/appMenu/appMenuList";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Long id) {
        if (id != null) {
            AppMenu appMenu = new AppMenu().setId(id);
            appMenu = baseService.getOne(new QueryWrapper<>(appMenu));
            request.setAttribute("entity", appMenu);
        }
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        request.setAttribute("uploadParam", uploadParam);
        return "sys/appMenu/appMenuDetail";
    }

    @GetMapping("/bandage/{id}")
    public ModelAndView bandage(@PathVariable(value = "id", required = false) Long id) {
        ModelAndView view = new ModelAndView("sys/appMenu/appMenuBandage");
        List<String> list = new ArrayList<String>() {{
            this.add("YHLX");
        }};
        List<Dict> dictList = dictService.getByCode(list);
        AppMenu appMenu = appMenuService.getById(id);
        QueryWrapper<AppMenuUserType> queryWrapper = new QueryWrapper<AppMenuUserType>().eq("name", appMenu.getName());
        List<AppMenuUserType> appMenuUserTypeList = appMenuUserTypeService.list(queryWrapper);
        view.addObject("appMenuUserTypeList", appMenuUserTypeList);
        view.addObject("appMenu",appMenu);
        view.addObject("dictList", dictList);
        view.addObject("id", id);
        return view;
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody AppMenuUserType appMenuUserType) {
        AppMenu appMenuById = appMenuService.getById(appMenuUserType.getId());
        QueryWrapper<AppMenuUserType> appMenuUserTypeQueryWrapper
                = new QueryWrapper<AppMenuUserType>().eq("name", appMenuById.getName()).eq("nav_code",appMenuById.getNavCode());
        int count = appMenuUserTypeService.count(appMenuUserTypeQueryWrapper);
        if (count > 0 ) {
            List<AppMenuUserType> list = appMenuUserTypeService.list(appMenuUserTypeQueryWrapper);
            Date createTime = list.get(0).getCreateTime();
            appMenuUserTypeService.remove(appMenuUserTypeQueryWrapper);
            AppMenu appMenu = appMenuService.getById(appMenuUserType.getId());
            appMenuUserType.setName(appMenu.getName());
            appMenuUserType.setNavCode(appMenu.getNavCode());
            String[] split = appMenuUserType.getDictValue().split(",");
            for (int i = 0; i < split.length; i++) {
                appMenuUserType.setCreateTime(createTime);
                appMenuUserType.setUpdateTime(new Date());
                appMenuUserType.setDictValue(split[i]);
                appMenuUserTypeService.save(appMenuUserType);
            }
        }else {
            AppMenu appMenu = appMenuService.getById(appMenuUserType.getId());
            appMenuUserType.setName(appMenu.getName());
            appMenuUserType.setNavCode(appMenu.getNavCode());
            String[] split = appMenuUserType.getDictValue().split(",");
            for (int i = 0; i < split.length; i++) {
                appMenuUserType.setCreateTime(new Date());
                appMenuUserType.setUpdateTime(new Date());
                appMenuUserType.setDictValue(split[i]);
                appMenuUserTypeService.save(appMenuUserType);
            }
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }
}
