package org.springblade.estate.feign;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.common.constant.OperationConstant;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.estate.dto.GuestAuthorizeDTO;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.estate.entity.GuestAuthorize;
import org.springblade.estate.vo.GuestAuthorizeVO;
import org.springblade.estate.wrapper.GuestAuthorizeWrapper;
import org.springblade.estate.service.IGuestAuthorizeService;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 访客授权记录 client
 *
 * @author lianghanqiang
 * @since 2021-05-08
 */

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

public class GuestAuthorizeClient implements IGuestAuthorizeClient {

    private final IGuestAuthorizeService guestAuthorizeService;

        /**
     * 详情
     */
    @Override
    @GetMapping("/detail")
    public GuestAuthorizeVO detail(@SpringQueryMap GuestAuthorize guestAuthorize) {
        GuestAuthorize detail = guestAuthorizeService.getOne(Condition.getQueryWrapper(guestAuthorize));
        return GuestAuthorizeWrapper.build().entityVO(detail);
        }

    /**
     * 查询 访客授权记录
     */
    @Override
    @GetMapping("/list")
    public List<GuestAuthorizeVO> list(@SpringQueryMap GuestAuthorize guestAuthorize) {
        return GuestAuthorizeWrapper.build().listVO(guestAuthorizeService.list(Condition.getQueryWrapper(guestAuthorize)));
        }

	@Override
	@GetMapping("/getListByUsers")
	public List<GuestAuthorizeVO> getListByUsers(@RequestParam List<Long> userIds) {
		List<GuestAuthorize> list = this.guestAuthorizeService.list(new QueryWrapper<>(new GuestAuthorize()).lambda().in(GuestAuthorize::getUserId, userIds));
		return GuestAuthorizeWrapper.build().listVO(list);
	}


	/**
     * 新增 访客授权记录
     */
    @Override
    @PostMapping("/save")
    public GuestAuthorize save(@Valid @RequestBody GuestAuthorize guestAuthorize) {
		boolean save = guestAuthorizeService.save(guestAuthorize);
		if(save){
			return guestAuthorize;
		}
		return null;
    }

    /**
     * 修改 访客授权记录
     */
    @Override
    @PostMapping("/update")
    public boolean update(@Valid @RequestBody GuestAuthorize guestAuthorize) {
        return guestAuthorizeService.updateById(guestAuthorize);
        }

    /**
     * 新增或修改 访客授权记录
     */
    @Override
    @PostMapping("/submit")
    public boolean submit(@Valid @RequestBody GuestAuthorize guestAuthorize) {
        return guestAuthorizeService.saveOrUpdate(guestAuthorize);
        }


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

    @Override
	@PostMapping("pageOfMonth")
    public BladePage<GuestAuthorize> pageOfMonth(@RequestBody GuestAuthorizeDTO guestAuthorizeDTO) {
		GuestAuthorize ga = new GuestAuthorize();
		BeanUtil.copyProperties(guestAuthorizeDTO, ga);
		DateTime now = DateUtil.date();
		DateTime offsetMonth = DateUtil.offsetMonth(now, -1);
		LambdaQueryWrapper<GuestAuthorize> queryWrapper = Condition.getQueryWrapper(ga)
			.lambda()
			.between(GuestAuthorize::getCreateTime,offsetMonth,now)
			.orderByDesc(GuestAuthorize::getCreateTime);
		IPage<GuestAuthorize> page = guestAuthorizeService.page(Condition.getPage(guestAuthorizeDTO.getQuery()),queryWrapper);
		return BladePage.of(page);
    }
}
