package org.springblade.estate.feign;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.estate.dto.AccessRecordCountDto;
import org.springblade.estate.dto.AccessRecordDTO;
import org.springblade.system.expand.feign.IConfClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.estate.entity.AccessRecord;
import org.springblade.estate.vo.AccessRecordVO;
import org.springblade.estate.wrapper.AccessRecordWrapper;
import org.springblade.estate.service.IAccessRecordService;

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

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

public class AccessRecordClient implements IAccessRecordClient {

    private final IAccessRecordService accessRecordService;
    private final IConfClient confClient;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public AccessRecordVO detail(@SpringQueryMap AccessRecord accessRecord) {
        AccessRecord detail = accessRecordService.getOne(Condition.getQueryWrapper(accessRecord));
        return AccessRecordWrapper.build(confClient).entityVO(detail);
        }

    /**
     * 查询
     */
    @Override
    @GetMapping("/list")
    public List<AccessRecordVO> list(@SpringQueryMap AccessRecord accessRecord) {
        return AccessRecordWrapper.build(confClient).listVO(accessRecordService.list(Condition.getQueryWrapper(accessRecord)));
        }



    /**
     * 新增
     */
    @Override
    @PostMapping("/save")
    public boolean save(@Valid @RequestBody AccessRecord accessRecord) {
        return accessRecordService.save(accessRecord);
        }

    /**
     * 修改
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody AccessRecord accessRecord) {
        return accessRecordService.updateById(accessRecord);
        }

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


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

	@Override
	@GetMapping("/count")
	public int count(AccessRecordVO accessRecord) {
		return accessRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new AccessRecord()).lambda()
			.eq(accessRecord.getAgencyId()!=null,AccessRecord::getAgencyId,accessRecord.getAgencyId())
			.eq(accessRecord.getEnterpriseId()!=null,AccessRecord::getEnterpriseId,accessRecord.getEnterpriseId())
			.eq(AccessRecord::getTenantId, AuthUtil.getUser().getTenantId())
			.eq(accessRecord.getResidentialId()!=null,AccessRecord::getResidentialId,accessRecord.getResidentialId())
			.eq(AccessRecord::getIsDeleted,0)
			.ge(accessRecord.getCreateTimeLike() == null,AccessRecord::getCreateTime, DateUtil.beginOfDay(new Date()))
			.like(accessRecord.getCreateTimeLike() != null, AccessRecord::getCreateTime,accessRecord.getCreateTimeLike())
		);
	}

	/**
	 * 出入记录数据统计
	 */
	@Override
	@GetMapping("/getAccessCount")
	public AccessRecordCountDto getAccessCount(){
		AccessRecordCountDto accessRecordCountDto = new AccessRecordCountDto();
		accessRecordCountDto.setDayCount(accessRecordService.selectDayAccessRecordCount());
		accessRecordCountDto.setWeekCount(accessRecordService.selectWeekAccessRecordCount());
		accessRecordCountDto.setMonthCount(accessRecordService.selectMonthAccessRecordCount());
		accessRecordCountDto.setYearCount(accessRecordService.selectYearAccessRecordCount());
		return accessRecordCountDto;
	}

	@Override
	@PostMapping("/generateAccessRecord")
	public R generateAccessRecord(@RequestBody AccessRecordDTO accessRecord) {
		return this.accessRecordService.generateAccessRecord(accessRecord);
	}

	@PostMapping("/page")
	@Override
	public BladePage page(@RequestBody AccessRecordDTO accessRecordDTO){
		IPage<AccessRecord> page = accessRecordService.page(Condition.getPage(accessRecordDTO.getQuery()), Condition.getQueryWrapper(accessRecordDTO));
		BladePage<AccessRecord> bladePage = BladePage.of(page);
		return bladePage;
	}


}
