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.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.custom.service.EquipmentService;
import com.tofly.watermeter.entity.Qfcbsbgl;
import com.tofly.watermeter.service.QfcbsbglService;
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 java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author FlashWJL
 * @date 2021/4/14 15:45
 */
@RestController
@AllArgsConstructor
@RequestMapping("/equi")
@Api(tags = "抄表--抄表设备管理")
public class EquipmentController {

    private final QfcbsbglService qfcbsbglService;
    private final EquipmentService equipmentService;


    /**
     * 分页查询
     * @param page 分页对象
     * @param inputContent 抄表业务设备管理表
     * @return
     */
    @GetMapping("/getEqui" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getQfcbsbglPage(Page page, String inputContent,String state) {
        return ResultRespone.success(qfcbsbglService.page(page, Wrappers.<Qfcbsbgl>lambdaQuery().orderByDesc(Qfcbsbgl::getId)
                .eq(StringUtil.isNotEmpty(state),Qfcbsbgl::getState,state)
                .and(StringUtil.isNotEmpty(inputContent) , v -> v.like(Qfcbsbgl::getSbbh , inputContent)
                        .or()
                        .like(Qfcbsbgl::getSbmc , inputContent)
                        .or()
                        .like(Qfcbsbgl::getSbxh , inputContent))));
    }

    /**
     * 新增抄表业务设备管理表
     * @param qfcbsbgl 抄表业务设备管理表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增抄表业务设备")
    @PostMapping("/saveEqui")
    public ResultRespone save(@RequestBody Qfcbsbgl qfcbsbgl) {
        return ResultRespone.success(equipmentService.saveEqui(qfcbsbgl));
    }


    /**
     * 修改抄表业务设备管理表
     * @param qfcbsbgl 抄表业务设备管理表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改抄表设备")
    @PutMapping("/updateEqui")
    public ResultRespone updateById(@RequestBody Qfcbsbgl qfcbsbgl) {
        String equiBh=qfcbsbgl.getSbbh();
        String equiMc=qfcbsbgl.getSbmc();
        String equiCj=qfcbsbgl.getSbcj();
        String equiXh=qfcbsbgl.getSbxh();
        String state= qfcbsbgl.getState();
        Long holdUser=qfcbsbgl.getCyry();
        Date lqsj = qfcbsbgl.getLqsj();
        //已发放
        if ("1".equals(state)){
            if (StringUtil.isNull(lqsj)){
                throw new ToflyDeniedException("请选择领取时间");
            }
            if (StringUtil.isNull(holdUser)){
                throw new ToflyDeniedException("请选择领取人员");
            }
        }
        if (StringUtil.isNotEmpty(equiBh)){
            //保存之前需要判断是否有相同的设备编码
            Qfcbsbgl sb = qfcbsbglService.getOne(Wrappers.<Qfcbsbgl>lambdaQuery().eq(Qfcbsbgl::getSbbh,equiBh).select(Qfcbsbgl::getId));
            if (StringUtil.isNotNull(sb)){
                throw new ToflyDeniedException("设备编号不能重复："+equiBh);
            }
        }
        return ResultRespone.success(qfcbsbglService.update(Wrappers.<Qfcbsbgl>lambdaUpdate()
                .eq(Qfcbsbgl::getId,qfcbsbgl.getId())
                .set(StringUtil.isNotEmpty(equiBh),Qfcbsbgl::getSbbh,equiBh)
                .set(StringUtil.isNotEmpty(equiMc),Qfcbsbgl::getSbmc,equiMc)
                .set(StringUtil.isNotEmpty(equiCj),Qfcbsbgl::getSbcj,equiCj)
                .set(StringUtil.isNotEmpty(equiXh),Qfcbsbgl::getSbxh,equiXh)
                .set(StringUtil.isNotEmpty(state),Qfcbsbgl::getState,state)
                .set("1".equals(state) , Qfcbsbgl::getLqsj , lqsj)
                .set("1".equals(state) , Qfcbsbgl::getCyry,holdUser)
                .set("0".equals(state) , Qfcbsbgl::getCyry,"")
                .set("0".equals(state) , Qfcbsbgl::getLqsj , "")));
    }

    @ApiOperation(value = "发放设备")
    @PutMapping("/grand")
    public ResultRespone grandById(@RequestBody Qfcbsbgl qfcbsbgl) {
        String sbbhS = qfcbsbgl.getSbbh();
        Long cyry = qfcbsbgl.getCyry();
        Date lqsj = qfcbsbgl.getLqsj();
        if (StringUtil.isNull(sbbhS)){
            throw new ToflyDeniedException("请选择设备");
        }
        if (StringUtil.isNull(lqsj)){
            throw new ToflyDeniedException("请选择领取时间");
        }
        if (StringUtil.isNull(cyry)){
            throw new ToflyDeniedException("请选择领取人员");
        }
        List<String> sbbhList = Arrays.asList(sbbhS.split(","));
        return ResultRespone.success(qfcbsbglService.update(Wrappers.<Qfcbsbgl>lambdaUpdate()
                .in(Qfcbsbgl::getSbbh , sbbhList).set(Qfcbsbgl::getCyry , cyry).set(Qfcbsbgl::getLqsj , lqsj)
                .set(Qfcbsbgl::getState , "1")));
    }

    /**
     * 通过id批量删除抄表业务设备管理表
     * @param ids id
     * @return ResultRespone
     */
    //="通过id批量删除抄表业务设备管理表" )
    @ApiOperation(value = "通过ID批量删除抄表业务设备管理表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "根据ID删除设备", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(equipmentService.removeById(ids));
    }


}
