/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.smartapplication.temperaturerecord.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.community.enterprise.entity.Enterprise;
import org.springblade.community.staff.service.IEnterpriseStaffService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.smartapplication.temperaturerecord.entity.TemperatureRecord;
import org.springblade.smartapplication.temperaturerecord.service.ITemperatureRecordService;
import org.springblade.smartapplication.temperaturerecord.vo.TemperatureRecordVO;
import org.springblade.smartapplication.temperaturerecord.wrapper.TemperatureRecordWrapper;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2020-12-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("smartapplication/temperaturerecord")
@Api(value = "测温记录", tags = "测温记录")
public class TemperatureRecordController extends BladeController {

	private ITemperatureRecordService temperatureRecordService;
	private IEnterpriseStaffService enterpriseStaffService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入temperatureRecord")
	public R<TemperatureRecordVO> detail(TemperatureRecord temperatureRecord) {
		TemperatureRecord detail = temperatureRecordService.getOne(Condition.getQueryWrapper(temperatureRecord));
		return R.data(TemperatureRecordWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入temperatureRecord")
	public R<IPage<TemperatureRecordVO>> list(@RequestParam Map<String, Object> temperatureRecord, Query query) {
		query.setDescs("recog_time");
		LambdaQueryWrapper<TemperatureRecord> wrapper = Condition.getQueryWrapper(temperatureRecord, TemperatureRecord.class).lambda();
		IPage<TemperatureRecord> pages = temperatureRecordService.page(Condition.getPage(query), wrapper);
		return R.data(TemperatureRecordWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入temperatureRecord")
	public R<IPage<TemperatureRecordVO>> page(TemperatureRecordVO temperatureRecord, Query query) {
		IPage<TemperatureRecordVO> pages = temperatureRecordService.selectTemperatureRecordPage(Condition.getPage(query), temperatureRecord);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入temperatureRecord")
	public R save(@Valid @RequestBody TemperatureRecord temperatureRecord) {
		return R.status(temperatureRecordService.save(temperatureRecord));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入temperatureRecord")
	public R update(@Valid @RequestBody TemperatureRecord temperatureRecord) {
		return R.status(temperatureRecordService.updateById(temperatureRecord));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入temperatureRecord")
	public R submit(@Valid @RequestBody TemperatureRecord temperatureRecord) {
		return R.status(temperatureRecordService.saveOrUpdate(temperatureRecord));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(temperatureRecordService.deleteLogic(Func.toLongList(ids)));
	}



	/********************************小程序接口*************************************/
	/**
	 * 园区小程序获取测温记录
	 */
	@GetMapping("/getTemperatureRecord")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "小程序获取测温记录", notes = "企业信用代码和员工手机号至少传一个")
	public R<IPage<TemperatureRecord>> getTemperatureRecord(TemperatureRecordVO temperatureRecord, Query query) {
		Integer isException = temperatureRecord.getIsException();
		if(temperatureRecord.getEnterpriseCreditCode() == null){//员工查询自己的测温记录
			if(temperatureRecord.getPhone() == null){
				if(temperatureRecord.getTenantId() != null){
					if(isException != null){
						IPage<TemperatureRecord> page = temperatureRecordService.page(Condition.getPage(query), new QueryWrapper<>(new TemperatureRecord()).lambda().eq(TemperatureRecord::getIsException,isException).eq(TemperatureRecord::getTenantId, temperatureRecord.getTenantId()).orderByDesc(TemperatureRecord::getRecogTime));
						return R.data(page);
					}else {
						IPage<TemperatureRecord> page = temperatureRecordService.page(Condition.getPage(query), new QueryWrapper<>(new TemperatureRecord()).lambda().eq(TemperatureRecord::getTenantId, temperatureRecord.getTenantId()).orderByDesc(TemperatureRecord::getRecogTime));
						return R.data(page);
					}

				}else{
					return R.fail("企业信用代码和员工手机号或园区id至少传一个");
				}

			}else{
				if(isException != null){
					IPage<TemperatureRecord> page = temperatureRecordService.page(Condition.getPage(query), new QueryWrapper<>(new TemperatureRecord()).lambda().eq(TemperatureRecord::getIsException,isException).eq(TemperatureRecord::getPhone, temperatureRecord.getPhone()).orderByDesc(TemperatureRecord::getRecogTime));
					return R.data(page);
				}else {
					IPage<TemperatureRecord> page = temperatureRecordService.page(Condition.getPage(query), new QueryWrapper<>(new TemperatureRecord()).lambda().eq(TemperatureRecord::getPhone, temperatureRecord.getPhone()).orderByDesc(TemperatureRecord::getRecogTime));
					return R.data(page);
				}

			}
		}else{//企业查询所有员工测温记录
			//根据企业信用代码获取员工列表
			List<String> phoneList = enterpriseStaffService.getListByEnterpriseCreditCode(temperatureRecord.getEnterpriseCreditCode());
			if(isException != null){
				IPage<TemperatureRecord> page = temperatureRecordService.page(Condition.getPage(query), new QueryWrapper<>(new TemperatureRecord()).lambda().eq(TemperatureRecord::getIsException,isException).in(TemperatureRecord::getPhone, phoneList).orderByDesc(TemperatureRecord::getRecogTime));
				return R.data(page);
			}else{
				IPage<TemperatureRecord> page = temperatureRecordService.page(Condition.getPage(query), new QueryWrapper<>(new TemperatureRecord()).lambda().in(TemperatureRecord::getPhone, phoneList).orderByDesc(TemperatureRecord::getRecogTime));
				return R.data(page);
			}

		}

	}

}
