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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.bmgl.controller.common.bmscyz;
import com.tofly.feesapi.bmgl.entity.BmSfqd;
import com.tofly.feesapi.bmgl.mapper.BmSfqdMapper;
import com.tofly.feesapi.bmgl.service.BmSfqdService;
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.apache.commons.lang.StringUtils;
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/bmsfqd")
@Api(tags="编码-收费渠道接口")
public class BmSfqdController {
    @Autowired
    private BmSfqdService bmSfqdService;
    @Autowired
    private BmSfqdMapper bmSfqdMapper;
    @Autowired
    private  bmscyz bmscyz;

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

    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页显示数据条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pages", value = "共几页,输出参数",  dataType = "int"),
            @ApiImplicitParam(name = "maxLimit", value = "每页最大显示数据",  dataType = "int"),
            @ApiImplicitParam(name = "orders[0].asc", value = "排序,bool类型",  dataType = "String"),
            @ApiImplicitParam(name = "orders[0].column", value = "排序,字段名",  dataType = "String"),
            @ApiImplicitParam(name = "searchText", value = "模糊查询(编码，名称，联系电话，备注，负责人，相关人员姓名)",  dataType = "String"),
            //@ApiImplicitParam(name = "isdisabled", value = "是否启用,0,1",  dataType = "String")
    })
    public ResultRespone getBmSfqdPage(Page page, String searchText) {
        //条件构造
        QueryWrapper<BmSfqd> queryWrapper = new QueryWrapper<BmSfqd>();
        if(StringUtils.isNotBlank(searchText)) {
            queryWrapper.like("name",searchText)
                    .or()
                    .like("xgryxm",searchText)
                    .or()
                    .like("code",searchText)
                    .or()
                    .like("lxdh",searchText)
                    .or()
                    .like("fzr",searchText)
                    .or()
                    .like("bz",searchText); //模糊查询
        }
        // 执行分页查询
        IPage<BmSfqd> sfqdPage = bmSfqdService.page(page,queryWrapper);
        // 遍历查询结果进行赋值
       // List<BmSfqd> sfqdList = sfqdPage.getRecords();
        //水表厂家查询
       // List<BmSfqd> sbcjList=bmSfqdService.list();
       // Map<String,String> map = new HashMap<>();
       // sbcjList.forEach(e->map.put(e.getCode().toString(), e.getName()));

        return ResultRespone.success(sfqdPage);
    }


    /**
     * 通过id查询收费渠道
     * @param code id
     * @return ResultRespone
     */
    @GetMapping("/{code}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "BigDecimal")
    })
    public ResultRespone getById(@PathVariable("code" ) BigDecimal code) {
      return ResultRespone.success(bmSfqdService.getById(code));
    }

    /**
     * 新增收费渠道
     * @param bmSfqd 收费渠道
     * @return ResultRespone
     */
    @ApiOperation(value = "新增收费渠道")
    @PostMapping
    public ResultRespone save(@RequestBody BmSfqd bmSfqd) {
         //添加
        BigDecimal num1=new BigDecimal(1);
        bmSfqd.setCode(bmSfqdMapper.selectMaxId().add(num1).toString());
        return ResultRespone.success(bmSfqdService.save(bmSfqd));

    }

    /**
     * 修改收费渠道
     * @param bmSfqd 收费渠道
     * @return ResultRespone
     */
    @ApiOperation(value = "修改收费渠道")
    @PutMapping
    public ResultRespone updateById(@RequestBody BmSfqd bmSfqd) {
      return ResultRespone.success(bmSfqdService.updateById(bmSfqd));
    }

    /**
     * 通过id删除收费渠道
     * @param code id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID删除收费渠道")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{code}" )
    public ResultRespone removeById(@PathVariable BigDecimal code) {
        if(bmscyz.IsczSSPJsy("jfqd",code.toString())){
            return ResultRespone.failed("收费渠道已使用，不可删除");
        }
      return ResultRespone.success(bmSfqdService.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 removeById(String ids) {
        if(bmscyz.IsczSSPJsy("jfqd",ids)){
            return ResultRespone.failed("收费渠道已使用，不可删除");
        }
        return ResultRespone.success(bmSfqdService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
