package com.xjrsoft.module.ledger.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.ledger.dto.AddLedgerConfigDto;
import com.xjrsoft.module.ledger.dto.LedgerConfigPageDto;
import com.xjrsoft.module.ledger.dto.UpdateLedgerConfigDto;
import com.xjrsoft.module.ledger.entity.LedgerConfig;
import com.xjrsoft.module.ledger.service.ILedgerConfigService;
import com.xjrsoft.module.ledger.vo.LedgerConfigPageVo;
import com.xjrsoft.module.ledger.vo.LedgerConfigVo;
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;

/**
* @title: 台账配置表
* @Author dzx
* @Date: 2024-03-06
* @Version 1.0
*/
@RestController
@RequestMapping("/ledger" + "/ledgerConfig")
@Api(value = "/ledger"  + "/ledgerConfig",tags = "台账配置表代码")
@AllArgsConstructor
public class LedgerConfigController {


    private final ILedgerConfigService ledgerConfigService;

    @GetMapping(value = "/page")
    @ApiOperation(value="台账配置表列表(分页)")
    @SaCheckPermission("ledgerconfig:detail")
    public RT<PageOutput<LedgerConfigPageVo>> page(@Valid LedgerConfigPageDto dto){
        LambdaQueryWrapper<LedgerConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
        .like(StrUtil.isNotEmpty(dto.getName()), LedgerConfig::getName, dto.getName())
        .orderByDesc(LedgerConfig::getId)
        .select(LedgerConfig.class,x -> VoToColumnUtil.fieldsToColumns(LedgerConfigPageVo.class).contains(x.getProperty()));
        IPage<LedgerConfig> page = ledgerConfigService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<LedgerConfigPageVo> pageOutput = ConventPage.getPageOutput(page, LedgerConfigPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/getPorcessId")
    @ApiOperation(value="获取processId")
    @SaCheckPermission("ledgerconfig:detail")
    public RT<String> getPorcessId(@RequestParam Long dto){
        return RT.ok("pageOutput");
    }
//    @GetMapping(value = "/list")
//    @ApiOperation(value = "学生考核项目列表(不分页)")
//    @SaCheckPermission("basestudentassessmentproject:detail")
//    public RT<List<LedgerConfigListVo>> list(@Valid LedgerConfigListDto dto) {
//
//        LambdaQueryWrapper<LedgerConfig> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper
//                .orderByDesc(LedgerConfig::getId)
//                .eq(LedgerConfig::getDeleteMark, DeleteMark.NODELETE.getCode())
//                .select(LedgerConfig.class, x -> VoToColumnUtil.fieldsToColumns(LedgerConfigListVo.class).contains(x.getProperty()));
//
//        List<LedgerConfig> list = ledgerConfigService.list(queryWrapper);
//        List<LedgerConfigListVo> listVos = BeanUtil.copyToList(list, LedgerConfigListVo.class);
//        return RT.ok(listVos);
//    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询台账配置表信息")
    @SaCheckPermission("ledgerconfig:detail")
    public RT<LedgerConfigVo> info(@RequestParam Long id){
        LedgerConfig ledgerConfig = ledgerConfigService.getById(id);
        if (ledgerConfig == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(ledgerConfig, LedgerConfigVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增台账配置表")
    @SaCheckPermission("ledgerconfig:add")
    public RT<Boolean> add(@Valid @RequestBody AddLedgerConfigDto dto){
        LedgerConfig ledgerConfig = BeanUtil.toBean(dto, LedgerConfig.class);
        boolean isSuccess = ledgerConfigService.save(ledgerConfig);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改台账配置表")
    @SaCheckPermission("ledgerconfig:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateLedgerConfigDto dto){
        LedgerConfig ledgerConfig = BeanUtil.toBean(dto, LedgerConfig.class);
        return RT.ok(ledgerConfigService.updateById(ledgerConfig));
    }

    @DeleteMapping
    @ApiOperation(value = "删除台账配置表")
    @SaCheckPermission("ledgerconfig:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(ledgerConfigService.removeBatchByIds(ids));
    }

}