package com.ssj.service.conch.curator.service.impl.v1;

import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.bean.weixin.libmy.domain.TbLibManagerAudit;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.conch.curator.service.v1.CuratorV1Service;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.weixin.library.service.LibManagerAuditService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class CuratorV1ServiceImpl implements CuratorV1Service {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IBookManagerService bookManagerService;
    @Autowired
    private LibManagerAuditService libManagerAuditService;

    @Override
    public ResponseEntity audit(int auditResultType, String libManagerAuditId, String reason) {
        ResponseEntity responseEntity = new ResponseEntity();

        if (1 == auditResultType) {
            //通过
            TbLibManagerAudit libManagerAudit = libManagerAuditService.getById(libManagerAuditId);
            if (libManagerAudit == null) {
                return responseEntity.failure("申请不存在");
            }

            libManagerAudit.setStatus(1);
            libManagerAudit.setLastUpdateTime(new Date());

            TbLibManager libManager = bookManagerService.getById(libManagerAudit.getLibManagerId());
            if (libManager == null) {
                logger.error("校长角色不存在id={}", libManagerAudit.getLibManagerId());
                return responseEntity.failure("数据异常");
            }
            libManager.setName(libManagerAudit.getName());
            libManager.setTelephone(libManagerAudit.getTelephone());
            libManager.setLibId(libManagerAudit.getLibId());
            libManager.setState(1);

            bookManagerService.save(libManager);
            libManagerAuditService.save(libManagerAudit);
        } else if (2 == auditResultType) {
            //不通过
            if (StringUtils.isBlank(reason)) {
                return responseEntity.failure("不通过需要填写原因");
            }

            TbLibManagerAudit libManagerAudit = libManagerAuditService.getById(libManagerAuditId);
            if (libManagerAudit == null) {
                return responseEntity.failure("申请不存在");
            }

            libManagerAudit.setStatus(2);
            libManagerAudit.setReason(reason);
            libManagerAudit.setLastUpdateTime(new Date());

            libManagerAuditService.save(libManagerAudit);
        } else {
            return responseEntity.failure("无效的审核类型");
        }

        return responseEntity.success("成功");
    }

}
	