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.organization.entity.Department;
import com.xjrsoft.module.outint.dto.AddVisitorOutInRecordDto;
import com.xjrsoft.module.outint.dto.UpdateVisitorOutInRecordDto;
import com.xjrsoft.module.outint.dto.VisitorOutInRecordPageDto;
import com.xjrsoft.module.outint.entity.StudentOutInRecord;
import com.xjrsoft.module.outint.entity.VisitorOutInRecord;
import com.xjrsoft.module.outint.service.IVisitorOutInRecordService;
import com.xjrsoft.module.outint.vo.VisitorOutInRecordPageVo;
import com.xjrsoft.module.outint.vo.VisitorOutInRecordVo;
import com.xjrsoft.module.personnel.entity.ReservationSchool;
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" + "/visitorOutInRecord")
@Api(value = "/outint"  + "/visitorOutInRecord",tags = "访客出入记录代码")
@AllArgsConstructor
public class VisitorOutInRecordController {


    private final IVisitorOutInRecordService visitorOutInRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="访客出入记录列表(分页)")
    @SaCheckPermission("visitoroutinrecord:detail")
    public RT<PageOutput<VisitorOutInRecordPageVo>> page(@Valid VisitorOutInRecordPageDto dto){
        IPage<VisitorOutInRecordPageVo> page = visitorOutInRecordService.selectJoinListPage(ConventPage.getPage(dto),VisitorOutInRecordPageVo.class,
                MPJWrappers.<VisitorOutInRecord>lambdaJoin()
                        .orderByDesc(VisitorOutInRecord::getId)
                        .like(StrUtil.isNotBlank(dto.getName()), ReservationSchoolPeople::getName,dto.getName())
                        .like(StrUtil.isNotBlank(dto.getIdentityCard()), ReservationSchoolPeople::getIdentityCard,dto.getIdentityCard())
                        .eq(ObjectUtil.isNotNull(dto.getDeptId())&&dto.getDeptId()!=0, ReservationSchool::getDeptId, dto.getDeptId())
                        .eq(ObjectUtil.isNotNull(dto.getRespondentUserId())&&dto.getRespondentUserId()!=0, ReservationSchool::getRespondentUserId, dto.getRespondentUserId())
                        .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.getStatus()), VisitorOutInRecord::getStatus, dto.getStatus())
                        .like(StrUtil.isNotBlank(dto.getPlateNumber()),ReservationSchoolPeople::getPlateNumber,dto.getPlateNumber())
                        .select(VisitorOutInRecord.class,x -> VoToColumnUtil.fieldsToColumns(VisitorOutInRecordPageVo.class).contains(x.getProperty()))
                        .select(ReservationSchoolPeople::getName,ReservationSchoolPeople::getIdentityCard,ReservationSchoolPeople::getPhone,ReservationSchoolPeople::getPlateNumber)
                        .select(ReservationSchool::getReason)
                        .leftJoin(ReservationSchoolPeople.class,ReservationSchoolPeople::getId,VisitorOutInRecord::getReservationSchoolPeopleId)
                        .leftJoin(ReservationSchool.class,ReservationSchool::getId,ReservationSchoolPeople::getReservationSchooId)
                        .leftJoin(XjrUser.class,XjrUser::getId,ReservationSchool::getRespondentUserId,ext->ext.selectAs(XjrUser::getName,VisitorOutInRecordPageVo::getRespondentUserCn))
                        .leftJoin(Department.class,Department::getId,ReservationSchool::getDeptId,ext->ext.selectAs(Department::getName,VisitorOutInRecordPageVo::getDeptCn))
                        .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode, ReservationSchoolPeople::getGender, ext->ext.selectAs(DictionaryDetail::getName, VisitorOutInRecordPageVo::getGender))
                );
        PageOutput<VisitorOutInRecordPageVo> pageOutput = ConventPage.getPageOutput(page, VisitorOutInRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询访客出入记录信息")
    @SaCheckPermission("visitoroutinrecord:detail")
    public RT<VisitorOutInRecordVo> info(@RequestParam Long id){
        VisitorOutInRecord visitorOutInRecord = visitorOutInRecordService.getById(id);
        if (visitorOutInRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(visitorOutInRecord, VisitorOutInRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增访客出入记录")
    @SaCheckPermission("visitoroutinrecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddVisitorOutInRecordDto dto){
        VisitorOutInRecord visitorOutInRecord = BeanUtil.toBean(dto, VisitorOutInRecord.class);
        boolean isSuccess = visitorOutInRecordService.save(visitorOutInRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改访客出入记录")
    @SaCheckPermission("visitoroutinrecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateVisitorOutInRecordDto dto){

        VisitorOutInRecord visitorOutInRecord = BeanUtil.toBean(dto, VisitorOutInRecord.class);
        return RT.ok(visitorOutInRecordService.updateById(visitorOutInRecord));

    }

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

    }

}