package com.tofly.watermeter.custom.controller.meterRead;

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.core.util.StringUtil;
import com.tofly.watermeter.custom.service.meterBusiness.export.ExportCommonCodeService;
import com.tofly.watermeter.entity.Qfkgjl;
import com.tofly.watermeter.service.QfkgjlService;
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.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author FlashWJL
 * @date 2021/8/18 14:22
 */
@RestController
@AllArgsConstructor
@RequestMapping("/switch")
@Api(tags = "抄表--开关记录")
public class SwitchRecordController {

    private final QfkgjlService qfkgjlService;
    private final ExportCommonCodeService exportCommonCodeService;


    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param input 搜索条件
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "开关记录查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "input", value = "输入框内容(模糊查询姓名、用户编号)",  dataType = "String"),
            @ApiImplicitParam(name = "isNew", value = "是否查询最新开关纪录；1：是查询最新数据(只返回一条数据)", dataType = "String"),
            @ApiImplicitParam(name = "deptId", value = "部门编号(精准查询)",  dataType = "long")
    })
    public ResultRespone getQfkgjlPage(HttpServletResponse response, Page<Qfkgjl> page, String input, String isNew, Long deptId, Long export) {

        if ("1".equals(isNew)) {
            //如果查询最新的数据，则设置分页只取第一条数据
            page.setSize(1);
        }

        if (export != null && export == 1) {
            List<Qfkgjl> list = qfkgjlService.list(Wrappers.<Qfkgjl>lambdaQuery()
                    //根据用户编号、用户姓名条件模糊查询
                    .and(StringUtil.isNotEmpty(input),
                            k -> k.like(Qfkgjl::getYhbh, input)
                                    .or().like(Qfkgjl::getYhxm, input)
                                    .or().like(Qfkgjl::getLxdh, input)
                                    .or().like(Qfkgjl::getTsbh, input)
                                    .or().like(Qfkgjl::getYhdz, input))
                    .eq(deptId != null, Qfkgjl::getCreateDept, deptId)
                    .orderByDesc(Qfkgjl::getCreateTime));
            exportCommonCodeService.exportCode(response, "switchRecordQueryService", list);
        }

        return ResultRespone.success(qfkgjlService.page(page, Wrappers.<Qfkgjl>lambdaQuery()
                //根据用户编号、用户姓名条件模糊查询
                .and(StringUtil.isNotEmpty(input),
                        k -> k.like(Qfkgjl::getYhbh, input)
                                .or().like(Qfkgjl::getYhxm, input)
                                .or().like(Qfkgjl::getLxdh, input)
                                .or().like(Qfkgjl::getTsbh, input)
                                .or().like(Qfkgjl::getYhdz, input))
                .eq(deptId != null, Qfkgjl::getCreateDept, deptId)
                .orderByDesc(Qfkgjl::getCreateTime)));

    }


    /**
     * 新增开关记录表
     *
     * @param qfkgjl 开关记录表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增开关记录表")
    @PostMapping("/saveSwitch")
    public ResultRespone save(@RequestBody Qfkgjl qfkgjl) {
        return ResultRespone.success(qfkgjlService.saveSwitch(qfkgjl));
    }


    /**
     * 修改开关记录表
     *
     * @param qfkgjl 开关记录表
     * @return ResultRespone
     */
//    @ApiOperation(value = "修改开关记录表")
//    @PutMapping
//    public ResultRespone updateById(@RequestBody Qfkgjl qfkgjl) {
//        return ResultRespone.success(qfkgjlService.updateById(qfkgjl));
//    }

    /**
     * 通过id删除开关记录表
     *
     * @param id id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID删除开关记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Long id) {
        return ResultRespone.success(qfkgjlService.removeById(id));
    }

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


}
