package com.tofly.feesapi.dzht.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.dzht.entity.HtTemplate;
import com.tofly.feesapi.dzht.service.HtTemplateService;
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.util.Arrays;

@RestController
@RequestMapping("/httemplate")
@Api(tags = "合同模版接口")
public class HtTemplateController {
    @Autowired
    private HtTemplateService htTemplateService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<HtTemplate>> getHtTemplatePage(Page page, @ModelAttribute HtTemplate htTemplate) {
        return ResultRespone.success(htTemplateService.getPage(page, htTemplate));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    public ResultRespone<HtTemplate> getById(@PathVariable("id") String id) {
        return ResultRespone.success(htTemplateService.getHtTemplateById(id));
    }

    @ApiOperation(value = "新增合同模版")
    @PostMapping(headers = "content-type=multipart/form-data")
    public ResultRespone save(HtTemplate htTemplate) {
        return ResultRespone.success(htTemplateService.HtSave(htTemplate));
    }

    @ApiOperation(value = "修改合同模版")
    @PutMapping(headers = "content-type=multipart/form-data")
    public ResultRespone updateById(HtTemplate htTemplate) {
        return ResultRespone.success(htTemplateService.HtUpdate(htTemplate));
    }

    @ApiOperation(value = "通过ID删除合同模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable String id) {
        if (htTemplateService.yzhtsfsy(id)) {
            throw new FeesException("该合同已使用，不能删除", FeesErrorCode.BUSINESS_ERROR);
        }
        return ResultRespone.success(htTemplateService.removeById(id));
    }

    @ApiOperation(value = "通过ID批量删除合同模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeByIds(String ids) {
        if (htTemplateService.yzhtsfsy(ids)) {
            throw new FeesException("存在合同已使用，不能删除", FeesErrorCode.BUSINESS_ERROR);
        }
        return ResultRespone.success(htTemplateService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
