package com.zhongshu.card.server.core.service.school;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.zhongshu.card.client.model.school.*;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.service.school.BookInfoService;
import com.zhongshu.card.client.type.OrganizationUserType;
import com.zhongshu.card.server.core.dao.org.DepartmentDao;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.dao.school.BookInfoDao;
import com.zhongshu.card.server.core.domain.org.Department;
import com.zhongshu.card.server.core.domain.school.BookInfo;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author TRX
 * @date 2024/6/7
 */
@Slf4j
@Service
public class BookInfoServiceImpl extends SuperService implements BookInfoService {

    @Autowired
    BookInfoDao bookInfoDao;

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    DepartmentDao departmentDao;

    @Autowired
    UserCountDao userCountDao;

    @Override
    public ResultContent addBookInfo(BookInfoAddParam param) {
        initDefaultUserAndOid(param);
        Assert.hasText(param.getPhone(), "phone不能为空");
        Assert.hasText(param.getIdCard(), "idCard不能为空");
        Assert.hasText(param.getName(), "name不能为空");
        String oid = param.getOid();
        OrganizationUserType userType = param.getUserType();

        BookInfo bookInfo = null;
        if (StringUtils.isEmpty(param.getId())) {
            bookInfo = bookInfoDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(bookInfo)) {
                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
            }
            BookInfo temp = bookInfoDao.findTopByPhoneAndOidAndUserType(param.getPhone(), oid, userType);
            if (ObjectUtils.isNotEmpty(temp) && temp.getId().equals(bookInfo.getId())) {
                return ResultContent.buildFail(String.format("%s 电话号码已存在", param.getPhone()));
            }
            temp = bookInfoDao.findTopByIdCardAndOidAndUserType(param.getIdCard(), oid, userType);
            if (ObjectUtils.isNotEmpty(temp) && temp.getId().equals(bookInfo.getId())) {
                return ResultContent.buildFail(String.format("%s 身份证号已存在", param.getIdCard()));
            }
        } else {
            bookInfo = new BookInfo();
            BookInfo temp = bookInfoDao.findTopByPhoneAndOidAndUserType(param.getPhone(), oid, userType);
            if (ObjectUtils.isNotEmpty(temp)) {
                return ResultContent.buildFail(String.format("%s 电话号码已存在", param.getPhone()));
            }
            temp = bookInfoDao.findTopByIdCardAndOidAndUserType(param.getIdCard(), oid, userType);
            if (ObjectUtils.isNotEmpty(temp)) {
                return ResultContent.buildFail(String.format("%s 身份证号已存在", param.getIdCard()));
            }
        }
        BeanUtils.copyProperties(param, bookInfo);
        if (StringUtils.isNotEmpty(param.getDepartmentId())) {
            Department department = departmentDao.findTopById(param.getDepartmentId());
            if (ObjectUtils.isEmpty(department)) {
                return ResultContent.buildFail(String.format("部门ID不存在： %s", param.getDepartmentId()));
            }
            bookInfo.setDepartment(department);
        }

        bookInfoDao.save(bookInfo);

        //todo 初始账号信息？
        initUserCountInfo(bookInfo);
        return ResultContent.buildSuccess();
    }

    public ResultContent initUserCountInfo(BookInfo bookInfo) {

        return ResultContent.buildSuccess();
    }

    @Override
    public ResultContent<Page<BookInfoModel>> page(BookInfoSearch param, Pageable pageable) {
        initOidSearchParam(param);
        Page<BookInfo> page = bookInfoDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    public ResultContent<BookInfoModel> getByDetailById(String id) {
        BookInfo bookInfo = bookInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(bookInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        BookInfoModel model = toModel(bookInfo);
        return ResultContent.buildSuccess(model);
    }

    public ResultContent deleteById(String id) {
        BookInfo bookInfo = bookInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(bookInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        bookInfoDao.delete(bookInfo);
        return ResultContent.buildSuccess();
    }

    public BookInfoModel toModel(BookInfo entity) {
        BookInfoModel model = new BookInfoModel();

        return model;
    }
}
