package com.hzs.system.sys.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApproveStatus;
import com.hzs.common.domain.system.sys.SysBusinessLog;
import com.hzs.system.sys.IBusinessLogApi;
import com.hzs.system.sys.dto.BusinessLogDTO;
import com.hzs.system.sys.service.ISysBusinessLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @Description: 业务操作明细dubbo服务
 * @Author: jiang chao
 * @Time: 2023/8/3 18:15
 * @Classname: BusinessLogServiceProvider
 * @PackageName: com.hzs.system.sys.provider
 */
@Slf4j
@DubboService
public class BusinessLogServiceProvider implements IBusinessLogApi {

    @Autowired
    private ISysBusinessLogService iSysBusinessLogService;

    @Override
    public R<String> saveBatch(List<BusinessLogDTO> dtoList) {
        try {
            if (CollectionUtil.isNotEmpty(dtoList)) {
                List<SysBusinessLog> sysBusinessLogList = new ArrayList<>(dtoList.size());

                for (BusinessLogDTO businessLogDTO : dtoList) {
                    SysBusinessLog sysBusinessLog = BeanUtil.copyProperties(businessLogDTO, SysBusinessLog.class);
                    if (businessLogDTO.getFreeSignFlag()) {
                        // 免签
                        sysBusinessLog.setBusinessStatus(EApproveStatus.FINISH.getValue());
                    } else {
                        // 走签呈
                        sysBusinessLog.setBusinessStatus(EApproveStatus.ALREADY_SUBMIT.getValue());
                    }
                    sysBusinessLogList.add(sysBusinessLog);
                }

                if (iSysBusinessLogService.saveBatch(sysBusinessLogList)) {
                    return R.ok();
                }
                return R.fail("保存入库数据失败");
            }
            return R.fail("保存入库数据为空");
        } catch (Exception e) {
            log.error("批量保存操作明细异常", e);
            return R.fail("保存入库数据异常: {}", e.getMessage());
        }
    }

}
