package com.xjrsoft.module.personnel.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
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.personnel.dto.AddCarManagementDto;
import com.xjrsoft.module.personnel.dto.CarManagementPageDto;
import com.xjrsoft.module.personnel.dto.UpdateCarManagementDto;
import com.xjrsoft.module.personnel.dto.UpdateCarManagementStatusDto;
import com.xjrsoft.module.personnel.entity.CarManagement;
import com.xjrsoft.module.personnel.service.ICarManagementService;
import com.xjrsoft.module.personnel.vo.CarManagementVo;
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;

@RestController
@RequestMapping("/personnel" + "/carmanager")
@Api(value = "/personnel"  + "/carmanager",tags = "车辆管理")
@AllArgsConstructor
public class CarManagementController {
    private  final ICarManagementService carManagementService;
    @GetMapping(value = "/page")
    @ApiOperation(value="车辆列表(分页)")
    @SaCheckPermission("carmanager:detail")
    public RT<PageOutput<CarManagementVo>> Page(@Valid CarManagementPageDto dto){
        MPJLambdaWrapper<CarManagement> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getCarNo()), CarManagement::getCarNo, dto.getCarNo())
                .like(ObjectUtil.isNotNull(dto.getCName()), CarManagement::getCName, dto.getCName())
                .like(StrUtil.isNotBlank(dto.getCPhone()), CarManagement::getCPhone, dto.getCPhone())
                .select(CarManagement.class,x -> VoToColumnUtil.fieldsToColumns(CarManagementVo.class).contains(x.getProperty()));

        IPage<CarManagement> page = carManagementService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<CarManagementVo> pageOutput = ConventPage.getPageOutput(page, CarManagementVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询车辆信息")
    @SaCheckPermission("carmanager:detail")
    public RT<CarManagementVo> info(@RequestParam Long id){
        CarManagement carManagement = carManagementService.getByIdDeep(id);
        if (carManagement == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(carManagement, CarManagementVo.class));
    }

    @PostMapping
    @ApiOperation(value = "新增车辆")
    @SaCheckPermission("carmanager:add")
    public RT<Boolean> add(@Valid @RequestBody AddCarManagementDto dto){
        CarManagement carManagement = BeanUtil.toBean(dto, CarManagement.class);
        return RT.ok(carManagementService.add(carManagement));

    }

    @PutMapping
    @ApiOperation(value = "修改车辆")
    @SaCheckPermission("carmanager:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateCarManagementDto dto){

        CarManagement carManagement = BeanUtil.toBean(dto, CarManagement.class);
        return RT.ok(carManagementService.update(carManagement));

    }

    @PutMapping("/status")
    @ApiOperation(value = "修改车辆状态")
    @SaCheckPermission("carmanager:edit-status")
    public RT<Boolean> updateEnabled(@Valid @RequestBody UpdateCarManagementStatusDto dto) {
        //根据id修改表单enabledMark
        return RT.ok(carManagementService.update(Wrappers.<CarManagement>update().lambda().set(CarManagement::getEnabledMark, dto.getEnabledMark()).eq(CarManagement::getId, dto.getId())));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("carmanager:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(carManagementService.delete(ids));
    }
}
