package com.pj.bank_info;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.pj.api.dto.BankInfoDto;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: bank_info -- 银行回执信息表
 *
 * @author lbl
 */
@RestController
@RequestMapping("/BankInfo/")
public class BankInfoController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    BankInfoService bankInfoService;

    /**
     * 增
     */
    @RequestMapping("add")
    @SaCheckPermission(BankInfo.PERMISSION_CODE_ADD)
    public AjaxJson add(BankInfo b) {
        bankInfoService.add(b);
        b = bankInfoService.getById(SP.publicMapper.getPrimarykey());
        return AjaxJson.getSuccessData(b);
    }

    /**
     * 删
     */
    @RequestMapping("delete")
    @SaCheckPermission(BankInfo.PERMISSION_CODE_DEL)
    public AjaxJson delete(Long id) {
        bankInfoService.delete(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 删 - 根据id列表
     */
    @RequestMapping("deleteByIds")
    @SaCheckPermission(BankInfo.PERMISSION_CODE_DEL)
    public AjaxJson deleteByIds() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        int line = SP.publicMapper.deleteByIds(BankInfo.TABLE_NAME, ids);
        return AjaxJson.getByLine(line);
    }

    /**
     * 改
     */
    @RequestMapping("update")
    @SaCheckPermission(BankInfo.PERMISSION_CODE_EDIT)
    public AjaxJson update(BankInfo b) {
        bankInfoService.update(b);
        return AjaxJson.getSuccess();
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    @SaCheckPermission(BankInfo.PERMISSION_CODE)
    public AjaxJson getById(Integer id) {
        BankInfo b = bankInfoService.getById(id);
        return AjaxJson.getSuccessData(b);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        String source = so.getString("source");
        if (StrUtil.isNotEmpty(source)) {
            source = StrUtil.subBefore(source, "_", false);
            so.put("source", source);
        }
        List<BankInfo> list = bankInfoService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    @PostMapping("rpc/getListBySource")
    public List<BankInfoDto> getListBySource(@RequestParam("source") String source) {
        SoMap so = new SoMap();
        so.put("source", source);
        List<BankInfo> list = bankInfoService.getList(so);
        List<BankInfoDto> dtoList = new ArrayList<>();
        list.forEach(item -> {
            BankInfoDto dto = new BankInfoDto();
            BeanUtils.copyProperties(item, dto);
            dtoList.add(dto);
        });
        return dtoList;
    }


}
