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.BizArchiveReceiveTicketFinished;
import cn.ezhizao.project.business.financialBase.service.IBizArchiveReceiveTicketFinishedService;
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 = "UploadTicketFinished数据接口")
@RequestMapping(value = "/business/uploadTicketFinished")
public class UploadTicketFinishedController extends BaseController {

    @Resource
    IBizArchiveReceiveTicketFinishedService uploadTicketFinishedService;

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

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

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

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

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

    @ApiOperation(value = "带条件List 分页查询",notes= "带条件List 分页查询")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult getList(@RequestBody Map<String,Object> 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 = uploadTicketFinishedService.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", uploadTicketFinishedService.getList(conditions));
        return success(map);
    }



}
