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.CarEnterRecord;
import org.springblade.estate.vo.CarEnterRecordVO;
import org.springblade.estate.wrapper.CarEnterRecordWrapper;
import org.springblade.estate.service.ICarEnterRecordService;
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/carenterrecord")

public class CarEnterRecordClient implements ICarEnterRecordClient {

    private final ICarEnterRecordService carEnterRecordService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public CarEnterRecordVO detail(@SpringQueryMap CarEnterRecord carEnterRecord) {
        CarEnterRecord detail = carEnterRecordService.getOne(Condition.getQueryWrapper(carEnterRecord));
        return CarEnterRecordWrapper.build().entityVO(detail);
        }

    /**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<CarEnterRecordVO> list(@SpringQueryMap CarEnterRecord carEnterRecord) {
        return CarEnterRecordWrapper.build().listVO(carEnterRecordService.list(Condition.getQueryWrapper(carEnterRecord)));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody CarEnterRecord carEnterRecord) {
        return carEnterRecordService.save(carEnterRecord);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody CarEnterRecord carEnterRecord) {
        return carEnterRecordService.updateById(carEnterRecord);
        }

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


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

	@Override
	@PostMapping("/count")
	public int count(@RequestBody CarEnterRecordVO carEnterRecordVO) {
		return carEnterRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new CarEnterRecord()).lambda()
			.eq(carEnterRecordVO.getAgencyId()!=null,CarEnterRecord::getAgencyId,carEnterRecordVO.getAgencyId())
			.eq(carEnterRecordVO.getEnterpriseId()!=null,CarEnterRecord::getEnterpriseId,carEnterRecordVO.getEnterpriseId())
			.eq(CarEnterRecord::getTenantId, AuthUtil.getUser().getTenantId())
			.eq(carEnterRecordVO.getResidentialId()!=null,CarEnterRecord::getResidentialId,carEnterRecordVO.getResidentialId())
			.eq(CarEnterRecord::getIsDeleted,0)
			.ge(CarEnterRecord::getCreateTime, DateUtil.beginOfDay(new Date()))
			.in(!carEnterRecordVO.isOutSide(),CarEnterRecord::getCarTypeNo, Arrays.asList("3648","3649","3650","3651"))
		    .notIn(carEnterRecordVO.isOutSide(), CarEnterRecord::getCarTypeNo, Arrays.asList("3648","3649","3650","3651")));
	}


}
