package  com.zcloud.modules.invoicing.controller;

import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.utils.*;
import  com.zcloud.modules.invoicing.entity.BusInvoicingReturnEntity;
import  com.zcloud.modules.invoicing.service.BusInvoicingReturnService;
import com.zcloud.modules.sys.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author lizenan
 * @Desc 开票管理回传发票(BusInvoicingReturn)表控制层
 * @Date 2024-04-08 08:55:58
 */
@Api(tags = "开票管理回传发票")
@RestController
@RequestMapping("busInvoicingReturn")
public class BusInvoicingReturnController extends AbstractController {

    @Autowired
    private BusInvoicingReturnService busInvoicingReturnService;

    /**
     *	新增修改时删除原有附件文件
     * @param entity	数据对象
     * @param returnFile		回传的PDF
     */
    private void deleteServerFile(BusInvoicingReturnEntity entity, MultipartFile returnFile) throws Exception {
        if (StringUtils.isNotBlank(entity.getFilepath()) && null != returnFile && !returnFile.isEmpty()) {
            FileUploadUtil.deleteFile(entity.getFilepath());
        }
    }

    /**
     *
     * @param entity	数据对象
     * @param file		身份证照片（正面）
     *
     * description:本地上传功能
     * String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE + Jurisdiction.getCORPINFO_ID() + "/" + ffile;
     * String fileName = FileUpload.fileUp(file, filePath, this.get32UUID());
     */
    private void handleInvoicingFile(BusInvoicingReturnEntity entity, MultipartFile file) throws Exception {
        if (null != file && !file.isEmpty()) {		 // 身份证照片（正面）
            String  ffile = DateUtil.getDays();
            String fileName = Tools.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String filePath = "/uploadFiles/invoicing/" + ffile;
            FileUploadUtil.sshSftp(file, fileName, filePath);
            entity.setFilepath(filePath+"/"+fileName);
        }
    }

    @ApiOperation("开票管理回传发票-编辑数据")
    @SysLog("开票管理回传发票-编辑数据")
    @PostMapping("/update")
    public R update(@RequestBody BusInvoicingReturnEntity param) {
        param.setOperator(getUserId());
        busInvoicingReturnService.updateById(param);

        return R.ok();
    }


    @ApiOperation("开票管理回传发票-删除数据")
    @SysLog("开票管理回传发票-删除数据")
    @PostMapping("/delete")
    public R delete(@RequestBody BusInvoicingReturnEntity param) {
        busInvoicingReturnService.delete(param.getInvoicingReturnId(),getUserId());
        return R.ok();
    }

    @ApiOperation("开票管理回传发票-分页查询")
    @SysLog("开票管理回传发票-分页查询")
    @PostMapping("/listPage")
    public R listPage(@RequestBody Map<String, Object> params) {
        PageUtils page = busInvoicingReturnService.listPage(params);

		return R.ok().put("page", page);
    }


    @ApiOperation("开票管理回传发票-根据ID查详情")
    @SysLog("开票管理回传发票-根据ID查详情")
    @PostMapping("/info")
    public R info(@RequestBody BusInvoicingReturnEntity param) {
        BusInvoicingReturnEntity busInvoicingReturn = busInvoicingReturnService.info(param.getInvoicingReturnId());
        return R.ok().put("data", busInvoicingReturn);
    }

}
