package com.xjrsoft.module.relationManage.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.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.relationManage.dto.AddDemoCustomerDto;
import com.xjrsoft.module.relationManage.dto.DemoCustomerPageDto;
import com.xjrsoft.module.relationManage.dto.UpdateDemoCustomerDto;
import com.xjrsoft.module.relationManage.entity.DemoCustomer;
import com.xjrsoft.module.relationManage.service.IDemoCustomerManageService;
import com.xjrsoft.module.relationManage.vo.DemoCustomerPageVo;
import com.xjrsoft.module.relationManage.vo.DemoCustomerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: demo_客户管理
* @Author 管理员
* @Date: 2025-03-11
* @Version 1.0
*/
@RestController
@RequestMapping("/relationManage" + "/demoCustomerManage")
@Api(value = "/relationManage"  + "/demoCustomerManage",tags = "demo_客户管理代码")
@AllArgsConstructor
public class DemoCustomerManageController {


private final IDemoCustomerManageService demoCustomerManageService;

    @GetMapping(value = "/page")
    @ApiOperation(value="DemoCustomer列表(分页)")
    @SaCheckPermission("demoCustomerManage:list")
    public R page(@Valid DemoCustomerPageDto dto){

    LambdaQueryWrapper<DemoCustomer> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper
            .like(StrUtil.isNotBlank(dto.getName()),DemoCustomer::getName,dto.getName())
            .like(StrUtil.isNotBlank(dto.getGender()),DemoCustomer::getGender,dto.getGender())
            .like(StrUtil.isNotBlank(dto.getPhone()),DemoCustomer::getPhone,dto.getPhone())
            .like(StrUtil.isNotBlank(dto.getEmail()),DemoCustomer::getEmail,dto.getEmail())
            .orderByDesc(DemoCustomer::getId)
    .select(DemoCustomer.class,x -> VoToColumnUtil.fieldsToColumns(DemoCustomerPageVo.class).contains(x.getProperty()));
    IPage<DemoCustomer> page = demoCustomerManageService.page(ConventPage.getPage(dto), queryWrapper);
    PageOutput<DemoCustomerPageVo> pageOutput = ConventPage.getPageOutput(page, DemoCustomerPageVo.class);
        return R.ok(pageOutput);
        }

        @GetMapping(value = "/info")
        @ApiOperation(value="根据id查询DemoCustomer信息")
        @SaCheckPermission("demoCustomerManage:detail")
        public R info(@RequestParam Long id){
        DemoCustomer demoCustomer = demoCustomerManageService.getById(id);
        if (demoCustomer == null) {
        return R.error("找不到此数据！");
        }
        return R.ok(BeanUtil.toBean(demoCustomer, DemoCustomerVo.class));
        }


        @PostMapping
        @ApiOperation(value = "新增DemoCustomer")
        @SaCheckPermission("demoCustomerManage:add")
        public R add(@Valid @RequestBody AddDemoCustomerDto dto){
            DemoCustomer demoCustomer = BeanUtil.toBean(dto, DemoCustomer.class);
            boolean isSuccess = demoCustomerManageService.save(demoCustomer);
        return R.ok(demoCustomer.getId());
        }

        @PutMapping
        @ApiOperation(value = "修改DemoCustomer")
        @SaCheckPermission("demoCustomerManage:edit")
        public R update(@Valid @RequestBody UpdateDemoCustomerDto dto){

            DemoCustomer demoCustomer = BeanUtil.toBean(dto, DemoCustomer.class);
            return R.ok(demoCustomerManageService.updateById(demoCustomer));

        }

        @DeleteMapping
        @ApiOperation(value = "删除")
        @SaCheckPermission("demoCustomerManage:delete")
        public R delete(@Valid @RequestBody List<Long> ids){
            return R.ok(demoCustomerManageService.removeBatchByIds(ids));

        }

                            }