package com.xjrsoft.module.app.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.annotation.XjrLog;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.app.dto.AddAppMenuDto;
import com.xjrsoft.module.app.dto.AppMenuListDto;
import com.xjrsoft.module.app.dto.AppMenuPageDto;
import com.xjrsoft.module.app.dto.UpdateAppMenuDto;
import com.xjrsoft.module.app.entity.AppMenu;
import com.xjrsoft.module.app.service.IAppMenuService;
import com.xjrsoft.module.app.vo.AppMenuListVo;
import com.xjrsoft.module.app.vo.AppMenuPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  app 菜单前端控制器
 * </p>
 *
 * @author tzx
 * @since 2023-07-26
 */
@RestController
@RequestMapping(GlobalConstant.APP_MODEL +"/menu")
@Api(value = GlobalConstant.APP_MODEL +"/menu", tags = "app 菜单模块")
@AllArgsConstructor
public class AppMenuController {

    private final IAppMenuService appMenuService;

    @GetMapping("/page")
    @ApiOperation("app菜单分页接口")
    public R page(AppMenuPageDto dto){

        LambdaQueryWrapper<AppMenu> queryWrapper = Wrappers.lambdaQuery(AppMenu.class)
                .eq(StrUtil.isNotBlank(dto.getName()), AppMenu::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getCode()), AppMenu::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getCategoryId()), AppMenu::getCategoryId, dto.getCategoryId())
                .orderByAsc(AppMenu::getSortCode)
                .select(AppMenu.class, x -> VoToColumnUtil.fieldsToColumns(AppMenuPageVo.class).contains(x.getProperty()));

        IPage<AppMenu> page = appMenuService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<AppMenuPageVo> pageOutput = ConventPage.getPageOutput(page, AppMenuPageVo.class);
        return R.ok(pageOutput);
    }


    @GetMapping("/list")
    @ApiOperation("app菜单 列表 不分页")
    public R list(AppMenuListDto dto){

        LambdaQueryWrapper<AppMenu> queryWrapper = Wrappers.lambdaQuery(AppMenu.class)
                .eq(StrUtil.isNotBlank(dto.getName()), AppMenu::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getCode()), AppMenu::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getCategoryId()), AppMenu::getCategoryId, dto.getCategoryId())
                .eq(AppMenu::getEnabledMark, EnabledMark.ENABLED.getCode())
                .orderByAsc(AppMenu::getSortCode)
                .select(AppMenu.class, x -> VoToColumnUtil.fieldsToColumns(AppMenuPageVo.class).contains(x.getProperty()));


        List<AppMenu> list = appMenuService.list(queryWrapper);

        List<AppMenuListVo> appMenuListVos = BeanUtil.copyToList(list, AppMenuListVo.class);

        return R.ok(appMenuListVos);
    }

    @PostMapping
    @ApiOperation("新增")
    public R add(@Valid @RequestBody AddAppMenuDto dto){

        AppMenu appMenu = BeanUtil.toBean(dto, AppMenu.class);

        return R.ok(appMenuService.save(appMenu));

    }


    @PutMapping
    @ApiOperation("修改")
    public R update(@Valid @RequestBody UpdateAppMenuDto dto){

        AppMenu appMenu = BeanUtil.toBean(dto, AppMenu.class);

        return R.ok(appMenuService.updateById(appMenu));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @XjrLog(value = "删除app端展示页面设计")
    public R delete(@Valid @RequestBody List<Long> ids) {
        appMenuService.removeBatchByIds(ids);
        return R.ok(Boolean.TRUE);
    }
}
