package com.xjrsoft.module.personnel.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.annotation.XjrLog;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.app.dto.UpdateEnableMarkDto;
import com.xjrsoft.module.app.entity.AppFuncDesign;
import com.xjrsoft.module.app.entity.AppMenu;
import com.xjrsoft.module.attendance.entity.StudentAttendanceRecord;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.personnel.dto.AddCarMessageApplyDto;
import com.xjrsoft.module.personnel.dto.UpdateCarMessageApplyDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.personnel.dto.CarMessageApplyPageDto;
import com.xjrsoft.module.personnel.entity.CarMessageApply;
import com.xjrsoft.module.personnel.entity.StundentFaceProcess;
import com.xjrsoft.module.personnel.entity.TeacherFaceProcess;
import com.xjrsoft.module.personnel.service.ICarMessageApplyService;
import com.xjrsoft.module.personnel.vo.CarMessageApplyPageVo;

import com.xjrsoft.module.personnel.vo.CarMessageApplyVo;
import com.xjrsoft.module.personnel.vo.StundentFaceProcessPageVo;
import com.xjrsoft.module.personnel.vo.TeacherFaceProcessPageVo;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
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.*;

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

/**
* @title: 车辆信息审核
* @Author dzx
* @Date: 2024-05-12
* @Version 1.0
*/
@RestController
@RequestMapping("/personnel" + "/carMessageApply")
@Api(value = "/personnel"  + "/carMessageApply",tags = "车辆信息审核代码")
@AllArgsConstructor
public class CarMessageApplyController {


    private final ICarMessageApplyService carMessageApplyService;

    @GetMapping(value = "/page")
    @ApiOperation(value="车辆信息审核列表(分页)")
    @SaCheckPermission("carmessageapply:detail")
    public RT<PageOutput<CarMessageApplyPageVo>> page(@Valid CarMessageApplyPageDto dto){

        MPJLambdaWrapper<CarMessageApply> queryWrapper =  MPJWrappers.<CarMessageApply>lambdaJoin()
                .orderByDesc(CarMessageApply::getId)
                .disableSubLogicDel()
                .like(StrUtil.isNotBlank(dto.getName()), XjrUser::getName,dto.getName())
                .like(StrUtil.isNotBlank(dto.getCarNumber()), CarMessageApply::getCarNumber,dto.getCarNumber())
                .eq(StrUtil.isNotBlank(dto.getUserName()), XjrUser::getUserName,dto.getUserName())
                .like(StrUtil.isNotBlank(dto.getMobile()),XjrUser::getMobile,dto.getMobile())
                .eq(ObjectUtil.isNotNull(dto.getStatus())&&dto.getStatus()!=0, CarMessageApply::getStatus,dto.getStatus())
                .select(CarMessageApply.class,x -> VoToColumnUtil.fieldsToColumns(CarMessageApplyPageVo.class).contains(x.getProperty()))
                .select(CarMessageApply::getId)
                .leftJoin(XjrUser.class,XjrUser::getId,CarMessageApply::getUserId, ext->ext.selectAs(XjrUser::getUserName, CarMessageApplyPageVo::getUserName).select(XjrUser::getMobile))
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,CarMessageApply::getGender, ext->ext.selectAs(DictionaryDetail::getName, CarMessageApplyPageVo::getGender))
                ;

        if(ObjectUtil.isNotNull(dto.getDepId())&&dto.getDepId()!=0) { // 要用用户关联部门查询
            queryWrapper.leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, CarMessageApply::getUserId)
                    .eq(UserDeptRelation::getDeptId, dto.getDepId());
        }

        IPage<CarMessageApplyPageVo> page = carMessageApplyService.selectJoinListPage(ConventPage.getPage(dto), CarMessageApplyPageVo.class,queryWrapper);
        PageOutput<CarMessageApplyPageVo> pageOutput = ConventPage.getPageOutput(page, CarMessageApplyPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/mobile-list")
    @ApiOperation(value="我的车辆(列表)")
    @SaCheckPermission("carmessageapply:detail")
    public RT<List<CarMessageApplyPageVo>> mobileList(){
        List<CarMessageApplyPageVo> list = carMessageApplyService.selectJoinList(CarMessageApplyPageVo.class,
                MPJWrappers.<CarMessageApply>lambdaJoin()
                        .orderByDesc(CarMessageApply::getId)
                        .eq(CarMessageApply::getUserId, StpUtil.getLoginIdAsLong())
                        .eq(CarMessageApply::getStatus,1)
                        .select(CarMessageApply::getId)
                        .select(CarMessageApply.class,x -> VoToColumnUtil.fieldsToColumns(CarMessageApplyPageVo.class).contains(x.getProperty())));
        return RT.ok(list);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询车辆信息审核信息")
    @SaCheckPermission("carmessageapply:detail")
    public RT<CarMessageApplyVo> info(@RequestParam Long id){
        CarMessageApply carMessageApply = carMessageApplyService.getById(id);
        if (carMessageApply == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(carMessageApply, CarMessageApplyVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增车辆信息审核")
    @SaCheckPermission("carmessageapply:add")
    public RT<Boolean> add(@Valid @RequestBody AddCarMessageApplyDto dto){
        CarMessageApply carMessageApply = BeanUtil.toBean(dto, CarMessageApply.class);
        boolean isSuccess = carMessageApplyService.save(carMessageApply);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改车辆信息审核")
    @SaCheckPermission("carmessageapply:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateCarMessageApplyDto dto){

        CarMessageApply carMessageApply = BeanUtil.toBean(dto, CarMessageApply.class);
        return RT.ok(carMessageApplyService.updateById(carMessageApply));

    }

    @DeleteMapping
    @ApiOperation(value = "删除车辆信息审核")
    @SaCheckPermission("carmessageapply:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(carMessageApplyService.removeBatchByIds(ids));

    }

    @PutMapping("/update-continue")
    @ApiOperation(value = "修改续期状态")
    @SaCheckPermission("carmessageapply:update-continue")
    public R updateContinue(@Valid @RequestBody UpdateEnableMarkDto dto) {
        CarMessageApply carMessageApply=new CarMessageApply();
        carMessageApply.setId(dto.getId());
        carMessageApply.setIsContinue(dto.getEnabledMark());
        carMessageApplyService.updateById(carMessageApply);
        return R.ok(true);
    }

}