package com.xjrsoft.module.personnel.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
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.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.AddPersonnelHealthyDto;
import com.xjrsoft.module.personnel.dto.PersonnelHealthyPageDto;
import com.xjrsoft.module.personnel.dto.UpdatePersonnelHealthyDto;
import com.xjrsoft.module.personnel.entity.PersonnelHealthy;
import com.xjrsoft.module.personnel.service.IPersonnelHealthyService;
import com.xjrsoft.module.personnel.vo.PersonnelHealthyPageVo;
import com.xjrsoft.module.personnel.vo.PersonnelHealthyVo;
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-07-23
* @Version 1.0
*/
@RestController
@RequestMapping("/personnel" + "/personnelHealthy")
@Api(value = "/personnel"  + "/personnelHealthy",tags = "人员健康信息表代码")
@AllArgsConstructor
public class PersonnelHealthyController {


    private final IPersonnelHealthyService personnelHealthyService;

    @GetMapping(value = "/page")
    @ApiOperation(value="人员健康信息表列表(分页)")
    @SaCheckPermission("personnelhealthy:detail")
    public RT<PageOutput<PersonnelHealthyPageVo>> page(@Valid PersonnelHealthyPageDto dto){

        Long userId = StpUtil.getLoginIdAsLong();
        if(dto.getUserId() != null){
            userId = dto.getUserId();
        }
        LambdaQueryWrapper<PersonnelHealthy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(PersonnelHealthy::getId)
                .eq(StrUtil.isNotEmpty(dto.getType()), PersonnelHealthy::getType, dto.getType())
                .eq(PersonnelHealthy::getUserId, userId)
                .select(PersonnelHealthy.class,x -> VoToColumnUtil.fieldsToColumns(PersonnelHealthyPageVo.class).contains(x.getProperty()));
        IPage<PersonnelHealthy> page = personnelHealthyService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<PersonnelHealthyPageVo> pageOutput = ConventPage.getPageOutput(page, PersonnelHealthyPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询人员健康信息表信息")
    @SaCheckPermission("personnelhealthy:detail")
    public RT<PersonnelHealthyVo> info(@RequestParam Long id){
        PersonnelHealthy personnelHealthy = personnelHealthyService.getById(id);
        if (personnelHealthy == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(personnelHealthy, PersonnelHealthyVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增人员健康信息表")
    @SaCheckPermission("personnelhealthy:add")
    public RT<Boolean> add(@Valid @RequestBody AddPersonnelHealthyDto dto){
        PersonnelHealthy personnelHealthy = BeanUtil.toBean(dto, PersonnelHealthy.class);
        boolean isSuccess = personnelHealthyService.save(personnelHealthy);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改人员健康信息表")
    @SaCheckPermission("personnelhealthy:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdatePersonnelHealthyDto dto){

        PersonnelHealthy personnelHealthy = BeanUtil.toBean(dto, PersonnelHealthy.class);
        return RT.ok(personnelHealthyService.updateById(personnelHealthy));

    }

    @DeleteMapping
    @ApiOperation(value = "删除人员健康信息表")
    @SaCheckPermission("personnelhealthy:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(personnelHealthyService.removeBatchByIds(ids));

    }

}