package com.xjrsoft.module.form.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.annotation.XjrLog;
import com.xjrsoft.common.constant.GlobalConstant;
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.form.dto.AddFormReleaseDto;
import com.xjrsoft.module.form.dto.FormReleasePageDto;
import com.xjrsoft.module.form.dto.UpdateFormReleaseDto;
import com.xjrsoft.module.form.entity.FormRelease;
import com.xjrsoft.module.form.entity.FormTemplate;
import com.xjrsoft.module.form.service.IFormReleaseService;
import com.xjrsoft.module.form.vo.FormReleasePageVo;
import com.xjrsoft.module.form.vo.FormReleaseVo;
import com.xjrsoft.module.system.entity.Menu;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 自定义表单 发布表 前端控制器
 * </p>
 *
 * @author tzx
 * @since 2022-05-09
 */
@RestController
@RequestMapping(GlobalConstant.FORM_MODULE_PREFIX + "/release")
@Api(value = GlobalConstant.FORM_MODULE_PREFIX + "/release", tags = "自定义表单发布")
@AllArgsConstructor
public class FormReleaseController {

    private final IFormReleaseService formReleaseService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "表单发布列表(分页)")
    @XjrLog(value = "获取表单发布列表")
    public R page(@Valid FormReleasePageDto dto) {
        //因为多表关联 会有多个表都使用了id字段，  所以必须专门指定主表的Id
        IPage<FormReleasePageVo> page = formReleaseService.selectJoinListPage(ConventPage.getPage(dto), FormReleasePageVo.class,
                MPJWrappers.<FormRelease>lambdaJoin()
                        .and(StrUtil.isNotBlank(dto.getKeyword()),
                                wrapper -> wrapper.like(Menu::getName, dto.getKeyword())
                                        .or().like(FormTemplate::getName, dto.getKeyword()))
                        .select(FormRelease::getId)
                        .select(FormRelease.class, x -> VoToColumnUtil.fieldsToColumns(FormReleasePageVo.class).contains(x.getProperty()))
                        .selectAs(Menu::getTitle,FormReleasePageVo::getMenuName)
                        .selectAs(FormTemplate::getName, FormReleasePageVo::getFormName)
                        .leftJoin(Menu.class, Menu::getId, FormRelease::getMenuId)
                        .leftJoin(FormTemplate.class, FormTemplate::getId, FormRelease::getFormId)
                        .orderByDesc(FormRelease::getCreateDate));

        PageOutput<FormReleasePageVo> pageOutput = ConventPage.getPageOutput(page);

        return R.ok(pageOutput);
    }


    @PostMapping
    @ApiOperation(value = "自定义表单发布")
    @XjrLog(value = "自定义表单发布")
    public R add(@Valid @RequestBody AddFormReleaseDto dto) {
        return R.ok(formReleaseService.addFormRelease(dto));
    }

    @PutMapping
    @ApiOperation(value = "修改自定义表单发布")
    @XjrLog(value = "自定义表单发布" )
    public R update(@Valid @RequestBody UpdateFormReleaseDto dto) {
        return R.ok(formReleaseService.updateFormRelease(dto));
    }


    @GetMapping(value = "/info")
    @ApiOperation(value = "表单发布详情")
    @XjrLog(value = "表单发布详情")
    public R info(@RequestParam Long id) {
        //因为多表关联 会有多个表都使用了id字段，  所以必须专门指定主表的Id
        FormRelease release = formReleaseService.getById(id);
        if (release == null) {
            return R.error("表单发布数据不存在");
        }

        return R.ok(BeanUtil.toBean(release, FormReleaseVo.class));
    }


    @DeleteMapping
    @ApiOperation(value = "删除")
    @XjrLog(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids) {
        return R.ok(formReleaseService.removeBatchByIds(ids));
    }

}
