package com.zhgd.xmgl.modules.basicdata.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.entity.AttendanceDev;
import com.zhgd.xmgl.modules.basicdata.service.IAttendanceDevService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Title: Controller
 * @Description: 考勤设备
 * @author： pengj
 * @date： 2023-03-14
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/attendanceDev")
@Slf4j
@Api(tags = "考勤设备管理")
public class AttendanceDevController {
    @Autowired
    private IAttendanceDevService attendanceDevService;

    /**
     * 分页列表查询
     * @return
     */
    @OperLog(operModul = "考勤设备管理", operType = "分页查询", operDesc = "分页列表查询考勤设备信息")
    @ApiOperation(value = " 分页列表查询考勤设备信息", notes = "分页列表查询考勤设备信息", httpMethod = "POST")
    @ApiImplicitParam(name = "devName", value = "设备名称", paramType = "query", dataType = "String")
    @PostMapping(value = "/page")
    public Result<IPage<AttendanceDev>> queryPageList(Page page, @RequestBody AttendanceDev attendanceDev) {
        LambdaQueryWrapper<AttendanceDev> wrapper = Wrappers.<AttendanceDev>lambdaQuery();
//        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(attendanceDev.getDevName())) {
            wrapper.like(AttendanceDev::getDevName, attendanceDev.getDevName());
        }
//        if (StringUtils.isNotBlank(user.getEngineeringSn())) {
//            wrapper.eq(AttendanceDev::getEngineeringSn, user.getEngineeringSn());
//        } else {
//            wrapper.eq(AttendanceDev::getProjectSn, user.getSn()).eq(AttendanceDev::getEngineeringSn, "");
//        }
        IPage<AttendanceDev> pageList = attendanceDevService.page(page, wrapper);
        return Result.success(pageList);
    }

    /**
     * 列表查询
     *
     * @param attendanceDev
     * @return
     */
    @OperLog(operModul = "考勤设备管理", operType = "列表查询", operDesc = "列表查询考勤设备信息")
    @ApiOperation(value = " 列表查询考勤设备信息", notes = "列表查询考勤设备信息", httpMethod = "POST")
    @ApiImplicitParam(name = "devName", value = "设备名称", paramType = "query", dataType = "String")
    @PostMapping(value = "/list")
    public Result<List<AttendanceDev>> queryList(@RequestBody AttendanceDev attendanceDev) {
        LambdaQueryWrapper<AttendanceDev> wrapper = Wrappers.<AttendanceDev>lambdaQuery();
//        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(attendanceDev.getDevName())) {
            wrapper.like(AttendanceDev::getDevName, attendanceDev.getDevName());
        }
//        if (StringUtils.isNotBlank(user.getEngineeringSn())) {
//            wrapper.eq(AttendanceDev::getEngineeringSn, user.getEngineeringSn());
//        } else {
//            wrapper.eq(AttendanceDev::getProjectSn, user.getSn()).eq(AttendanceDev::getEngineeringSn, "");
//        }
        List<AttendanceDev> list = attendanceDevService.list(wrapper);
        return Result.success(list);
    }


    /**
     * 添加
     *
     * @param attendanceDev
     * @return
     */
    @OperLog(operModul = "考勤设备管理", operType = "新增", operDesc = "添加考勤设备信息")
    @ApiOperation(value = " 添加考勤设备信息", notes = "添加考勤设备信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody AttendanceDev attendanceDev) {
        SecurityUser user = SecurityUtil.getUser();
        attendanceDev.setEngineeringSn(user.getEngineeringSn());
        attendanceDev.setProjectSn(user.getSn());
        attendanceDevService.saveInfo(attendanceDev);
        return Result.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param attendanceDev
     * @return
     */
    @OperLog(operModul = "考勤设备管理", operType = "修改", operDesc = "编辑考勤设备信息")
    @ApiOperation(value = "编辑考勤设备信息", notes = "编辑考勤设备信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<AttendanceDev> edit(@RequestBody AttendanceDev attendanceDev) {
        Result<AttendanceDev> result = new Result<AttendanceDev>();
        AttendanceDev attendanceDevEntity = attendanceDevService.getById(attendanceDev.getId());
        if (attendanceDevEntity == null) {
            result.error500("未找到对应实体");
        } else {
            SecurityUser user = SecurityUtil.getUser();
            attendanceDev.setEngineeringSn(user.getEngineeringSn());
            attendanceDev.setProjectSn(user.getSn());
            boolean ok = attendanceDevService.updateInfo(attendanceDev);
            if (ok) {
                result.success("修改成功!");
            }
            result.success("操作失败!");
        }

        return result;
    }

    /**
     * 通过id删除
     * @return
     */
    @OperLog(operModul = "考勤设备管理", operType = "删除", operDesc = "删除考勤设备信息")
    @ApiOperation(value = "删除考勤设备信息", notes = "删除考勤设备信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "考勤设备ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/delete")
    public Result<AttendanceDev> delete(@RequestBody AttendanceDev attendanceDev) {
        Result<AttendanceDev> result = new Result<AttendanceDev>();
        AttendanceDev attendanceDevEntity = attendanceDevService.getById(attendanceDev.getId());
        if (attendanceDevEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = attendanceDevService.delInfo(attendanceDevEntity);
            if (ok) {
                result.success("删除成功!");
            }
            result.success("操作失败!");
        }

        return result;
    }


    /**
     * 通过id查询
     * @return
     */
    @OperLog(operModul = "考勤设备管理", operType = "查询", operDesc = "通过id查询考勤设备信息")
    @ApiOperation(value = "通过id查询考勤设备信息", notes = "通过id查询考勤设备信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "考勤设备ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<AttendanceDev> queryById(@RequestBody AttendanceDev attendanceDevEntity) {
        Result<AttendanceDev> result = new Result<AttendanceDev>();
        AttendanceDev attendanceDev = attendanceDevService.getById(attendanceDevEntity.getId());
        if (attendanceDev == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(attendanceDev);
            result.setSuccess(true);
        }
        return result;
    }
}
