package com.xjrsoft.module.outint.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.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.LocalDateTimeUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.outint.dto.AddCarOutInRecordDto;
import com.xjrsoft.module.outint.dto.CarOutInRecordPageDto;
import com.xjrsoft.module.outint.dto.UpdateCarOutInRecordDto;
import com.xjrsoft.module.outint.entity.CarOutInRecord;
import com.xjrsoft.module.outint.entity.StudentOutInRecord;
import com.xjrsoft.module.outint.service.ICarOutInRecordService;
import com.xjrsoft.module.outint.vo.CarOutInRecordPageVo;
import com.xjrsoft.module.outint.vo.CarOutInRecordVo;
import com.xjrsoft.module.personnel.entity.CarManagement;
import com.xjrsoft.module.personnel.entity.CarMessageApply;
import com.xjrsoft.module.personnel.entity.ReservationSchoolPeople;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.teacher.entity.XjrUser;
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-05-14
* @Version 1.0
*/
@RestController
@RequestMapping("/outint" + "/carOutInRecord")
@Api(value = "/outint"  + "/carOutInRecord",tags = "车辆出入记录代码")
@AllArgsConstructor
public class CarOutInRecordController {


    private final ICarOutInRecordService carOutInRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="车辆出入记录列表(分页)")
    @SaCheckPermission("caroutinrecord:detail")
    public RT<PageOutput<CarOutInRecordPageVo>> page(@Valid CarOutInRecordPageDto dto){

        IPage<CarOutInRecordPageVo> page = carOutInRecordService.selectJoinListPage(ConventPage.getPage(dto),CarOutInRecordPageVo.class,
                MPJWrappers.<CarOutInRecord>lambdaJoin()
                        .orderByDesc(CarOutInRecord::getId)
                        .disableSubLogicDel()
                        .between((ObjectUtil.isNotNull(dto.getStartTime()) && ObjectUtil.isNotNull(dto.getStartTime())), StudentOutInRecord::getRecordTime, LocalDateTimeUtil.format(dto.getStartTime(),"yyyy-MM-dd")+" 00:00:00", LocalDateTimeUtil.format(dto.getEndTime(),"yyyy-MM-dd")+" 23:59:59")
                        .eq(ObjectUtil.isNotNull(dto.getCategory()), CarOutInRecord::getCategory, dto.getCategory())
                        .eq(ObjectUtil.isNotNull(dto.getStatus()), CarOutInRecord::getStatus, dto.getStatus())
                        .eq(ObjectUtil.isNotNull(dto.getVehicleType()), CarMessageApply::getVehicleType, dto.getVehicleType())
                        .like(StrUtil.isNotBlank(dto.getName()), CarMessageApply::getName,dto.getName())

                        .like(StrUtil.isNotBlank(dto.getPhone()),XjrUser::getMobile,dto.getPhone())
                        .select(CarOutInRecord::getId)

                        .select(CarOutInRecord.class,x -> VoToColumnUtil.fieldsToColumns(CarOutInRecordPageVo.class).contains(x.getProperty()))
                        .selectAs(CarOutInRecord::getPlanNo,CarOutInRecordPageVo::getPlateNumber)

                        .select(CarMessageApply::getVehicleType)
                        .leftJoin(CarMessageApply.class,CarMessageApply::getId,CarOutInRecord::getCarMessageApplyId,ext->ext
                                .select(CarMessageApply::getName))
                        .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, CarMessageApply::getGender, ext->ext.selectAs(DictionaryDetail::getName, CarOutInRecordPageVo::getGender))
                        .leftJoin(XjrUser.class,XjrUser::getId,CarMessageApply::getUserId,ext->ext.selectAs(XjrUser::getMobile,CarOutInRecordPageVo::getPhone)
                                .selectAs(XjrUser::getCredentialNumber,CarOutInRecordPageVo::getIdentityCard))

                );

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询车辆出入记录信息")
    @SaCheckPermission("caroutinrecord:detail")
    public RT<CarOutInRecordVo> info(@RequestParam Long id){
        CarOutInRecord carOutInRecord = carOutInRecordService.getById(id);
        if (carOutInRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(carOutInRecord, CarOutInRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增车辆出入记录")
    @SaCheckPermission("caroutinrecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddCarOutInRecordDto dto){
        CarOutInRecord carOutInRecord = BeanUtil.toBean(dto, CarOutInRecord.class);
        boolean isSuccess = carOutInRecordService.save(carOutInRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改车辆出入记录")
    @SaCheckPermission("caroutinrecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateCarOutInRecordDto dto){

        CarOutInRecord carOutInRecord = BeanUtil.toBean(dto, CarOutInRecord.class);
        return RT.ok(carOutInRecordService.updateById(carOutInRecord));

    }

    @DeleteMapping
    @ApiOperation(value = "删除车辆出入记录")
    @SaCheckPermission("caroutinrecord:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(carOutInRecordService.removeBatchByIds(ids));

    }

}