package cn.ezhizao.project.business.financialBase.controller;

import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.project.business.financialBase.domain.BizFinancialDictionary;
import cn.ezhizao.project.business.financialBase.service.IBizFinancialDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * @author 周晓
 */
@RestController
@Api(tags = "FinancialDictionary数据接口")
@RequestMapping(value = "/business/financialDictionary")
public class FinancialDictionaryController extends BaseController {

    @Resource
    IBizFinancialDictionaryService financialDictionaryService;

    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @GetMapping("/getById/{id}")
    @ResponseBody
    public AjaxResult getById(@PathVariable Long id) {
        return success(financialDictionaryService.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存", notes = "保存")
    @ResponseBody
    public AjaxResult save(@RequestBody BizFinancialDictionary financialDictionary) {
        return (financialDictionary.getId() == null ? financialDictionaryService.save(financialDictionary) : financialDictionaryService.updateById(financialDictionary)) ? success() : error();
    }

    @PostMapping("/saveBatch")
    @ApiOperation(value = "批量保存", notes = "批量保存")
    @ResponseBody
    public AjaxResult saveBatch(@RequestBody List<BizFinancialDictionary> list) {
        return financialDictionaryService.saveOrUpdateBatch(list) ? success() : error();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @GetMapping("/deleteById/{id}")
    @ResponseBody
    public AjaxResult deleteById(@PathVariable Long id) {
        return financialDictionaryService.removeById(id) ? success() : error();
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @PostMapping("/batchDelete")
    @ResponseBody
    public AjaxResult batchDelete(@RequestBody List<Long> list) {
        return financialDictionaryService.removeBatchByIds(list) ? success() : error();
    }

    @ApiOperation(value = "带条件List 分页查询", notes = "带条件List 分页查询")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult getList(@RequestBody Map<String, Object> conditions) {
//        conditions = setMainAccountSetsId(conditions);
        Map<String, Object> map = new HashMap<>(16);
        Integer pn = conditions.get("pn") == null ? null : Integer.parseInt(conditions.get("pn").toString());
        Integer size = conditions.get("size") == null ? null : Integer.parseInt(conditions.get("size").toString());

        //总条数
        int total = financialDictionaryService.getTotal(conditions);
        map.put("total", total);
        Integer start = (pn == null || size == null) ? null : (pn - 1) * size >= total ? 0 : (pn - 1) * size;
        conditions.put("start", start);
        if (Objects.equals(start, 0)) {
            map.put("pn", 1);
        }
        //分页数据
        map.put("list", financialDictionaryService.getList(conditions));
        return success(map);
    }

}
