package com.bs.dc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import com.bs.dc.domain.DcVisitRecord;
import com.bs.dc.service.IDcVisitRecordService;
import javax.annotation.Resource;

/**
 * 访客访问记录Controller
 *
 * @author bs
 * @date 2025-10-09
 */
@Api(tags = "访客访问记录")
@RestController
@RequestMapping("/visit/record")
public class DcVisitRecordController extends BaseController {
    @Resource
    private IDcVisitRecordService dcVisitRecordService;

    /**
     * 分页查询访客访问记录列表
     */
    @ApiOperation("分页查询访客访问记录列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(DcVisitRecord dcVisitRecord) {
        startPage();
        LambdaQueryWrapper<DcVisitRecord> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dcVisitRecord);
        List<DcVisitRecord> list = dcVisitRecordService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询访客访问记录列表
     */
    @ApiOperation("查询访客访问记录列表")
    @GetMapping("/list")
    public AjaxResult list(DcVisitRecord dcVisitRecord) {
        LambdaQueryWrapper<DcVisitRecord> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dcVisitRecord);
        List<DcVisitRecord> list = dcVisitRecordService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出访客访问记录列表
     */
    @ApiOperation("导出访客访问记录列表")
    @Log(title = "访客访问记录导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DcVisitRecord dcVisitRecord) {
        LambdaQueryWrapper<DcVisitRecord> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,dcVisitRecord);
        List<DcVisitRecord> list = dcVisitRecordService.list(queryWrapper);
        ExcelUtil<DcVisitRecord> util = new ExcelUtil<DcVisitRecord>(DcVisitRecord. class);
        util.exportExcel(response, list, "访客访问记录数据");
    }

    /**
     * 获取访客访问记录详细信息
     */
    @ApiOperation("获取访客访问记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(dcVisitRecordService.getById(id));
    }

    /**
     * 新增访客访问记录
     */
    @ApiOperation("新增访客访问记录")
    @Log(title = "访客访问记录新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DcVisitRecord dcVisitRecord) {
        return toAjax(dcVisitRecordService.save(dcVisitRecord));
    }

    /**
     * 修改访客访问记录
     */
    @ApiOperation("修改访客访问记录")
    @Log(title = "访客访问记录修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DcVisitRecord dcVisitRecord) {
        return toAjax(dcVisitRecordService.updateById(dcVisitRecord));
    }

    /**
     * 删除访客访问记录
     */
    @ApiOperation("删除访客访问记录")
    @Log(title = "访客访问记录删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(dcVisitRecordService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<DcVisitRecord> queryWrapper,DcVisitRecord dcVisitRecord){


        //Id
        if(StringUtils.isNotEmpty(dcVisitRecord.getId())){
            queryWrapper.eq(DcVisitRecord::getId,dcVisitRecord.getId());
        }

        //访客记录Id
        if(StringUtils.isNotEmpty(dcVisitRecord.getVisitId())){
            queryWrapper.eq(DcVisitRecord::getVisitId,dcVisitRecord.getVisitId());
        }

        //访客单号
        if(StringUtils.isNotEmpty(dcVisitRecord.getVisitNumber())){
            queryWrapper.eq(DcVisitRecord::getVisitNumber,dcVisitRecord.getVisitNumber());
        }

        //姓名
        if(StringUtils.isNotEmpty(dcVisitRecord.getName())){
            queryWrapper.eq(DcVisitRecord::getName,dcVisitRecord.getName());
        }

        //性别
        if(StringUtils.isNotEmpty(dcVisitRecord.getSex())){
            queryWrapper.eq(DcVisitRecord::getSex,dcVisitRecord.getSex());
        }

        //证件类型
        if(StringUtils.isNotEmpty(dcVisitRecord.getCertType())){
            queryWrapper.eq(DcVisitRecord::getCertType,dcVisitRecord.getCertType());
        }

        //证件号码
        if(StringUtils.isNotEmpty(dcVisitRecord.getCertNumber())){
            queryWrapper.eq(DcVisitRecord::getCertNumber,dcVisitRecord.getCertNumber());
        }

        //单位名称
        if(StringUtils.isNotEmpty(dcVisitRecord.getUnitName())){
            queryWrapper.eq(DcVisitRecord::getUnitName,dcVisitRecord.getUnitName());
        }

        //单位Id
        if(StringUtils.isNotEmpty(dcVisitRecord.getUnitId())){
            queryWrapper.eq(DcVisitRecord::getUnitId,dcVisitRecord.getUnitId());
        }

        //联系电话
        if(StringUtils.isNotEmpty(dcVisitRecord.getContactPhone())){
            queryWrapper.eq(DcVisitRecord::getContactPhone,dcVisitRecord.getContactPhone());
        }

        //车牌号码
        if(StringUtils.isNotEmpty(dcVisitRecord.getVesselNumber())){
            queryWrapper.eq(DcVisitRecord::getVesselNumber,dcVisitRecord.getVesselNumber());
        }

        //联系地址
        if(StringUtils.isNotEmpty(dcVisitRecord.getContactAddr())){
            queryWrapper.eq(DcVisitRecord::getContactAddr,dcVisitRecord.getContactAddr());
        }

        //入厂状态0未入厂1已入厂
        if(StringUtils.isNotEmpty(dcVisitRecord.getInStatus())){
            queryWrapper.eq(DcVisitRecord::getInStatus,dcVisitRecord.getInStatus());
        }

        //入厂日期
        if(null != (dcVisitRecord.getInDate())){
            queryWrapper.eq(DcVisitRecord::getInDate,dcVisitRecord.getInDate());
        }

        //入厂操作人员
        if(StringUtils.isNotEmpty(dcVisitRecord.getInOperator())){
            queryWrapper.eq(DcVisitRecord::getInOperator,dcVisitRecord.getInOperator());
        }

        //出厂状态0未出厂 1已出厂
        if(StringUtils.isNotEmpty(dcVisitRecord.getOutStatus())){
            queryWrapper.eq(DcVisitRecord::getOutStatus,dcVisitRecord.getOutStatus());
        }

        //出厂日期
        if(null != (dcVisitRecord.getOutDate())){
            queryWrapper.eq(DcVisitRecord::getOutDate,dcVisitRecord.getOutDate());
        }

        //出厂操作人员
        if(StringUtils.isNotEmpty(dcVisitRecord.getOutOperator())){
            queryWrapper.eq(DcVisitRecord::getOutOperator,dcVisitRecord.getOutOperator());
        }

        //同一组访客id
        if(StringUtils.isNotEmpty(dcVisitRecord.getVisitNumberTotal())){
            queryWrapper.eq(DcVisitRecord::getVisitNumberTotal,dcVisitRecord.getVisitNumberTotal());
        }

        //备注
        if(StringUtils.isNotEmpty(dcVisitRecord.getRemark())){
            queryWrapper.eq(DcVisitRecord::getRemark,dcVisitRecord.getRemark());
        }

        //创建人员
        if(null != (dcVisitRecord.getCreateBy())){
            queryWrapper.eq(DcVisitRecord::getCreateBy,dcVisitRecord.getCreateBy());
        }

        //创建时间
        if(null != (dcVisitRecord.getCreateTime())){
            queryWrapper.eq(DcVisitRecord::getCreateTime,dcVisitRecord.getCreateTime());
        }

        //修改人员
        if(null != (dcVisitRecord.getUpdateBy())){
            queryWrapper.eq(DcVisitRecord::getUpdateBy,dcVisitRecord.getUpdateBy());
        }

        //修改时间
        if(null != (dcVisitRecord.getUpdateTime())){
            queryWrapper.eq(DcVisitRecord::getUpdateTime,dcVisitRecord.getUpdateTime());
        }

        }

}
