package com.tofly.feesapi.bmgl.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.tofly.feesapi.common.NumberHelper;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.bmgl.controller.common.bmscyz;
import com.tofly.feesapi.bmgl.entity.BmYslx;
import com.tofly.feesapi.bmgl.entity.dto.BmYslxFilter;
import com.tofly.feesapi.bmgl.entity.vo.BmYslxSelect;
import com.tofly.feesapi.bmgl.mapper.BmYslxMapper;
import com.tofly.feesapi.bmgl.service.BmYslxService;
import com.tofly.feesapi.common.core.dto.PageDto;
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.common.model.CollectionData;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 用水类型
 */
@RestController
@RequestMapping("/api/bmgl/bmyslx")
@Api(tags = "用水类型")
public class BmYslxController {

    @Autowired
    private BmYslxService bmYslxService;
    @Autowired
    private BmYslxMapper bmYslxMapper;
    @Autowired
    private bmscyz bmscyz;

    @GetMapping("/list")
    @ApiOperation(value = "收费渠道列表list")
    public ResultRespone getyslxList() {
        List<Map<String, Object>> list = bmYslxMapper.selectListMap();
        return ResultRespone.success(new CollectionData(list, list.size()));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<PageResult<BmYslxSelect>> getPage(PageDto pageDto, @ModelAttribute BmYslxFilter filter) {
        return ResultRespone.success(bmYslxService.getPage(pageDto, filter));
    }

    @GetMapping("/{code}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", required = true, dataType = "BigDecimal")
    })
    public ResultRespone<BmYslx> getById(@PathVariable("code") String code) {
        return ResultRespone.success(bmYslxService.getById(code));
    }

    @ApiOperation(value = "新增用水类型")
    @PostMapping
    public ResultRespone<Boolean> save(@RequestBody BmYslx bmYslx) {
        BigDecimal num1 = new BigDecimal(1);
        bmYslx.setCode(bmYslxMapper.selectMaxId().add(num1).toString());
        BigDecimal znj = bmYslx.getZnj();
        if (ObjUtil.isNull(znj)) {
            bmYslx.setZnj(BigDecimal.ZERO);
        }
        return ResultRespone.success(bmYslxService.save(bmYslx));
    }

    @ApiOperation(value = "修改用水类型")
    @PutMapping
    public ResultRespone<Boolean> updateById(@RequestBody BmYslx bmYslx) {
        String code = bmYslx.getCode();
        if (StrUtil.isEmpty(code)) {
            throw new FeesException("用水类型ID不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
        BmYslx original = bmYslxService.getById(code);
        String[] nullPropertyNames = NumberHelper.getNullPropertyNames(bmYslx);
        BeanUtil.copyProperties(bmYslx, original, nullPropertyNames);
        return ResultRespone.success(bmYslxService.updateById(original));
    }

    @ApiOperation(value = "通过ID删除用水类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{code}")
    public ResultRespone<Boolean> removeById(@PathVariable String code) {
        if (bmscyz.IsczSJsy("yslx", code)) {
            return ResultRespone.failed("该用水类型已使用，不可删除");
        }
        return ResultRespone.success(bmYslxService.removeById(code));
    }

    /**
     * 通过id批量删除用水类型
     *
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID批量删除用水类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone<Boolean> removeByIds(String ids) {
        if (bmscyz.IsczSJsy("yslx", ids)) {
            return ResultRespone.failed("该用水类型已使用，不可删除");
        }
        return ResultRespone.success(bmYslxService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
