package com.xjrsoft.module.dataexpert.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.dataexpert.dto.AddDataExpertSourceDto;
import com.xjrsoft.module.dataexpert.dto.DataExpertSourceFieldListDto;
import com.xjrsoft.module.dataexpert.dto.DataExpertSourcePageDto;
import com.xjrsoft.module.dataexpert.dto.UpdateDataExpertSourceDto;
import com.xjrsoft.module.dataexpert.entity.DataExpertSource;
import com.xjrsoft.module.dataexpert.service.IDataExpertSourceService;
import com.xjrsoft.module.dataexpert.vo.DataExpertSourceFieldVo;
import com.xjrsoft.module.dataexpert.vo.DataExpertSourcePageVo;
import com.xjrsoft.module.dataexpert.vo.DataExpertSourceVo;
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.Date;
import java.util.List;

/**
* @title: 数据导出-数据源设置
* @Author dzx
* @Date: 2024-04-19
* @Version 1.0
*/
@RestController
@RequestMapping("/dataexpert" + "/dataExpertSource")
@Api(value = "/dataexpert"  + "/dataExpertSource",tags = "数据导出-数据源设置代码")
@AllArgsConstructor
public class DataExpertSourceController {


    private final IDataExpertSourceService dataExpertSourceService;

    @GetMapping(value = "/page")
    @ApiOperation(value="数据导出-数据源设置列表(分页)")
    @SaCheckPermission("dataexpertsource:detail")
    public RT<PageOutput<DataExpertSourcePageVo>> page(@Valid DataExpertSourcePageDto dto){

        LambdaQueryWrapper<DataExpertSource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(DataExpertSource::getId)
                .select(DataExpertSource.class,x -> VoToColumnUtil.fieldsToColumns(DataExpertSourcePageVo.class).contains(x.getProperty()));
        IPage<DataExpertSource> page = dataExpertSourceService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<DataExpertSourcePageVo> pageOutput = ConventPage.getPageOutput(page, DataExpertSourcePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/list")
    @ApiOperation(value="数据导出-数据源设置列表")
    @SaCheckPermission("dataexpertsource:detail")
    public RT<List<DataExpertSourceVo>> list(@Valid DataExpertSourcePageDto dto){

        LambdaQueryWrapper<DataExpertSource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(DataExpertSource::getId)
                .select(DataExpertSource.class,x -> VoToColumnUtil.fieldsToColumns(DataExpertSourcePageVo.class).contains(x.getProperty()));
        List<DataExpertSource> list = dataExpertSourceService.list(queryWrapper);
        return RT.ok(BeanUtil.copyToList(list, DataExpertSourceVo.class));
    }

    @GetMapping(value = "/info-source-type")
    @ApiOperation(value="根据sourceType查询数据导出-数据源设置信息")
    @SaCheckPermission("dataexpertsource:detail")
    public RT<DataExpertSourceVo> list(@RequestParam String sourceType){
        DataExpertSource dataExpertSource = dataExpertSourceService.getOne(
                new QueryWrapper<DataExpertSource>().lambda()
                .eq(DataExpertSource::getSourceType, sourceType)
        );
        if (dataExpertSource == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(dataExpertSource, DataExpertSourceVo.class));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询数据导出-数据源设置信息")
    @SaCheckPermission("dataexpertsource:detail")
    public RT<DataExpertSourceVo> info(@RequestParam Long id){
        DataExpertSource dataExpertSource = dataExpertSourceService.getByIdDeep(id);
        if (dataExpertSource == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(dataExpertSource, DataExpertSourceVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增数据导出-数据源设置")
    @SaCheckPermission("dataexpertsource:add")
    public RT<Boolean> add(@Valid @RequestBody AddDataExpertSourceDto dto){
        DataExpertSource dataExpertSource = BeanUtil.toBean(dto, DataExpertSource.class);
        dataExpertSource.setCreateDate(new Date());
        boolean isSuccess = dataExpertSourceService.add(dataExpertSource);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改数据导出-数据源设置")
    @SaCheckPermission("dataexpertsource:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateDataExpertSourceDto dto){

        DataExpertSource dataExpertSource = BeanUtil.toBean(dto, DataExpertSource.class);
        dataExpertSource.setModifyDate(new Date());
        return RT.ok(dataExpertSourceService.update(dataExpertSource));

    }

    @DeleteMapping
    @ApiOperation(value = "删除数据导出-数据源设置")
    @SaCheckPermission("dataexpertsource:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(dataExpertSourceService.delete(ids));
    }


    @GetMapping(value = "/field-list")
    @ApiOperation(value="数据源字段")
    @SaCheckPermission("dataexpertsource:field-list")
    public RT<List<DataExpertSourceFieldVo>> fieldList(@Valid DataExpertSourceFieldListDto dto){
        return RT.ok(dataExpertSourceService.getFieldList(dto));
    }
}