package org.springblade.estate.feign;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.estate.entity.CarOutRecord;
import org.springblade.estate.vo.CarOutRecordVO;
import org.springblade.estate.wrapper.CarOutRecordWrapper;
import org.springblade.estate.service.ICarOutRecordService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 *  client
 *
 * @author lianghanqiang
 * @since 2021-03-30
 */

@AllArgsConstructor
@RestController
@RequestMapping("/feign/caroutrecord")

public class CarOutRecordClient implements ICarOutRecordClient {

    private final ICarOutRecordService carOutRecordService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public CarOutRecordVO detail(@SpringQueryMap CarOutRecord carOutRecord) {
        CarOutRecord detail = carOutRecordService.getOne(Condition.getQueryWrapper(carOutRecord));
        return CarOutRecordWrapper.build().entityVO(detail);
        }

    /**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<CarOutRecordVO> list(@SpringQueryMap CarOutRecord carOutRecord) {
        return CarOutRecordWrapper.build().listVO(carOutRecordService.list(Condition.getQueryWrapper(carOutRecord)));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody CarOutRecord carOutRecord) {
        return carOutRecordService.save(carOutRecord);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody CarOutRecord carOutRecord) {
        return carOutRecordService.updateById(carOutRecord);
        }

    /**
     * 新增或修改
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody CarOutRecord carOutRecord) {
        return carOutRecordService.saveOrUpdate(carOutRecord);
        }


    /**
     * 删除
     */
    @Override
    @PostMapping("/remove")
    public boolean remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return carOutRecordService.deleteLogic(Func.toLongList(ids));
        }

       /**
			* 统计当日
	 		* @param carOutRecordVO
	 		* @return
		 */
	@Override
	@PostMapping("/count")
	public int count(@RequestBody CarOutRecordVO carOutRecordVO) {
		return carOutRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new CarOutRecord()).lambda()
			.eq(carOutRecordVO.getAgencyId()!=null,CarOutRecord::getAgencyId,carOutRecordVO.getAgencyId())
			.eq(carOutRecordVO.getEnterpriseId()!=null,CarOutRecord::getEnterpriseId,carOutRecordVO.getEnterpriseId())
			.eq(CarOutRecord::getTenantId, AuthUtil.getUser().getTenantId())
			.eq(carOutRecordVO.getResidentialId()!=null,CarOutRecord::getResidentialId,carOutRecordVO.getResidentialId())
			.eq(CarOutRecord::getIsDeleted,0)
			.in(carOutRecordVO.isOutSide(), CarOutRecord::getCarTypeNo,Arrays.asList("3648","3649","3650","3651"))
			.notIn(!carOutRecordVO.isOutSide(),CarOutRecord::getCarTypeNo, Arrays.asList("3648","3649","3650","3651"))
			.ge(CarOutRecord::getCreateTime, DateUtil.beginOfDay(new Date())));

	}


}
