package com.tofly.feesapi.sfgl.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.sfgl.entity.Plsfkhxx;
import com.tofly.feesapi.sfgl.entity.vo.PlsfkhxxDeleteReq;
import com.tofly.feesapi.sfgl.entity.vo.PlsfkhxxPageReq;
import com.tofly.feesapi.sfgl.entity.vo.PlsfkhxxReq;
import com.tofly.feesapi.sfgl.service.PlsfkhxxService;
import com.tofly.feesapi.yhgl.entity.vo.KhxxBdReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/plsfkhxx")
@Api(tags = "批量收费客户信息管理")
public class PlsfkhxxController {

    @Autowired
    private PlsfkhxxService plsfkhxxService;

    @ApiOperation(value = "添加批量收费客户信息")
    @PostMapping("/addkhxx")
    public ResultRespone<Void> add(@RequestBody PlsfkhxxReq plsfkhxxReq) {
        plsfkhxxService.add(plsfkhxxReq);
        return ResultRespone.success();
    }

    @ApiOperation(value = "修改客户信息")
    @PostMapping("/updatekhxx")
    public ResultRespone<Void> update(@RequestBody Plsfkhxx update) {
        if (StrUtil.isEmpty(update.getId())) {
            throw new FeesException("客户信息id不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
        update.setSfkp("1");
        plsfkhxxService.updateById(update);
        return ResultRespone.success();
    }

    @ApiOperation(value = "删除客户信息")
    @PostMapping("/deletekhxx")
    public ResultRespone<Void> delete(@RequestBody PlsfkhxxDeleteReq deleteReq) {
        if (StrUtil.isEmpty(deleteReq.getId())) {
            throw new FeesException("客户信息id不能为空", FeesErrorCode.BUSINESS_ERROR);
        }

        plsfkhxxService.removeById(deleteReq.getId());
        return ResultRespone.success();
    }

    @ApiOperation(value = "查询客户信息")
    @GetMapping("/detail/{id}")
    @ApiImplicitParam(name = "id", value = "客户信息id")
    public ResultRespone<Plsfkhxx> detail(@PathVariable("id") String id) {
        Plsfkhxx byId = plsfkhxxService.getById(id);
        if (ObjUtil.isNotNull(byId)) {
            List<String> list = StrUtil.split(byId.getCbbbms(), StrUtil.COMMA);
            byId.setCbbbmList(list);
        }
        return ResultRespone.success();
    }

    @PostMapping("/khxxpage")
    @ApiOperation(value = "查询客户信息分页")
    public ResultRespone<PageResult<Plsfkhxx>> page(@RequestBody PlsfkhxxPageReq plsfkhxxPageReq) {
        return ResultRespone.success(plsfkhxxService.selectPage(plsfkhxxPageReq));
    }

    @PostMapping("/bindkhxx")
    @ApiOperation(value = "客户信息绑定抄表本")
    public ResultRespone<Void> bind(@RequestBody KhxxBdReq bdReq) {
        plsfkhxxService.bind(bdReq);
        return ResultRespone.success();
    }

}
