package com.xjrsoft.module.print.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.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.print.dto.AddPrintSchemaDto;
import com.xjrsoft.module.print.dto.EnabledPrintSchemaDto;
import com.xjrsoft.module.print.dto.PrintSchemaListDto;
import com.xjrsoft.module.print.dto.PrintSchemaPageDto;
import com.xjrsoft.module.print.dto.UpdatePrintSchemaDto;
import com.xjrsoft.module.print.entity.PrintSchema;
import com.xjrsoft.module.print.service.IPrintSchemaService;
import com.xjrsoft.module.print.vo.PrintSchemaInfoVo;
import com.xjrsoft.module.print.vo.PrintSchemaListVo;
import com.xjrsoft.module.print.vo.PrintSchemaPageVo;
import com.xjrsoft.module.system.entity.Menu;
import com.xjrsoft.module.system.service.IMenuService;
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 2023-05-19
 */
@RestController
@RequestMapping(GlobalConstant.PRINT_MODULE_PREFIX)
@Api(value = GlobalConstant.PRINT_MODULE_PREFIX, tags = "打印模块")
@AllArgsConstructor
public class PrintSchemaController {

    private final IPrintSchemaService printSchemaService;

    private final IMenuService menuService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "打印模板不分页")
    public R list(PrintSchemaListDto dto) {

        LambdaQueryWrapper<PrintSchema> queryWrapper = Wrappers.lambdaQuery(PrintSchema.class)
                .eq(ObjectUtil.isNotNull(dto.getCategory()), PrintSchema::getCategory, dto.getCategory())
                .like(StrUtil.isNotBlank(dto.getName()), PrintSchema::getName, dto.getName())
                .like(StrUtil.isNotBlank(dto.getCode()), PrintSchema::getCode, dto.getCode())
                .eq(PrintSchema::getEnabledMark, EnabledMark.ENABLED.getCode())
                .select(PrintSchema.class, x -> VoToColumnUtil.fieldsToColumns(PrintSchemaListVo.class).contains(x.getProperty()));
        List<PrintSchema> list = printSchemaService.list(queryWrapper);

        List<PrintSchemaListVo> printSchemaListVos = BeanUtil.copyToList(list, PrintSchemaListVo.class);
        return R.ok(printSchemaListVos);
    }


    @GetMapping(value = "/page")
    @ApiOperation(value = "打印模板分页")
    public R page(PrintSchemaPageDto dto) {

        LambdaQueryWrapper<PrintSchema> queryWrapper = Wrappers.lambdaQuery(PrintSchema.class)
                .eq(ObjectUtil.isNotNull(dto.getCategory()), PrintSchema::getCategory, dto.getCategory())
                .like(StrUtil.isNotBlank(dto.getName()), PrintSchema::getName, dto.getName())
                .like(StrUtil.isNotBlank(dto.getCode()), PrintSchema::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getEnabledMark()), PrintSchema::getEnabledMark, dto.getEnabledMark())
                .select(PrintSchema.class, x -> VoToColumnUtil.fieldsToColumns(PrintSchemaPageVo.class).contains(x.getProperty()));
        IPage<PrintSchema> page = printSchemaService.page(ConventPage.getPage(dto), queryWrapper);

        PageOutput<PrintSchemaPageVo> pageOutput = ConventPage.getPageOutput(page, PrintSchemaPageVo.class);
        return R.ok(pageOutput);
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取详情")
    public R info(@RequestParam Long id){
        PrintSchema schema = printSchemaService.getById(id);

        PrintSchemaInfoVo printSchemaInfoVo = BeanUtil.toBean(schema, PrintSchemaInfoVo.class);

        if (ObjectUtil.isNotNull(schema.getIsMenu())) {
            Menu menu = menuService.getOne(Wrappers.lambdaQuery(Menu.class).eq(Menu::getId,schema.getMenuId()).select(Menu::getIcon,Menu::getParentId));
            if (ObjectUtil.isNotNull(menu)) {
                printSchemaInfoVo.setIcon(menu.getIcon());
                printSchemaInfoVo.setParentId(menu.getParentId());
            }

        }

        return R.ok(printSchemaInfoVo);
    }


    @PostMapping
    @ApiOperation(value = "新增")
    public R add(@Valid @RequestBody AddPrintSchemaDto dto){

        return R.ok(printSchemaService.add(dto));
    }



    @PutMapping
    @ApiOperation(value = "修改")
    public R update(@Valid @RequestBody UpdatePrintSchemaDto dto){

        return R.ok(printSchemaService.updateSchema(dto));
    }

    @PutMapping("/enabled")
    @ApiOperation(value = "启用/禁用")
    public R enabled(@Valid @RequestBody EnabledPrintSchemaDto dto){
        PrintSchema schema = new PrintSchema();
        schema.setId(dto.getId());
        schema.setEnabledMark(dto.getEnabledMark());

        return R.ok(printSchemaService.updateById(schema));
    }


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