/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.entity.Userandplot;
import com.tofly.xrtygis.entity.dto.UserandplotDto;
import com.tofly.xrtygis.service.UserandplotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 小区-用户关联表
 *
 * @author chenhao
 * @date Thu Mar 31 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/userAndPlot")
@Api(tags = "小区-用户关联表接口")
public class UserandplotController {

    private final UserandplotService userandplotService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param userandplot 小区-用户关联表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getUserandplotPage(Page page, @ModelAttribute Userandplot userandplot) {
        return ResultRespone.success(userandplotService.page(page, Wrappers.<Userandplot>lambdaQuery()
                .eq(Userandplot::getPlotId, userandplot.getPlotId())
                .like(!StringUtils.isEmpty(userandplot.getUserCode()), Userandplot::getUserCode, userandplot.getUserCode())));
    }

    /**
     * 新增小区-用户关联表
     *
     * @return ResultRespone
     */
    @ApiOperation(value = "新增小区-用户关联表")
    @PostMapping
    public ResultRespone save(@RequestBody UserandplotDto dto) {
        return ResultRespone.success(userandplotService.bindUserAndPlot(dto));
    }

    @GetMapping("/plotCount")
    @ApiOperation("小区客户数统计")
    @ApiImplicitParam(name = "plotIds",value = "小区id，分割，空统计所有绑定小区")
    public ResultRespone plotCount(String plotIds){
        return ResultRespone.success(userandplotService.plotCount(plotIds));
    }

    /**
     * 修改小区-用户关联表
     *
     * @return ResultRespone
     */
    @ApiOperation(value = "修改小区-用户关联表")
    @PutMapping
    public ResultRespone updateById(String ids, Long plotId) {

        return ResultRespone.success(userandplotService.update(Wrappers.<Userandplot>lambdaUpdate()
                .in(Userandplot::getId, Arrays.asList(ids.split(",")))
                .set(Userandplot::getPlotId, plotId)));
    }

    /**
     * 通过id批量删除小区-用户关联表
     *
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id批量删除小区-用户关联表")
    @ApiOperation(value = "通过ID批量删除小区-用户关联表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(userandplotService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @GetMapping("/getUserInfo")
    @ApiOperation("获取未绑定用户")
    public ResultRespone getUserInfo(Page page,String dto) {
        return ResultRespone.success(userandplotService.getUserInfo(page,dto));
    }
}
