package com.app.service.guardian;

import com.app.dto.PageResult;
import com.app.dto.account.search.SearchAccountDto;
import com.app.entity.guardian.Guardian;
import com.app.mapper.guardian.GuardianMapper;
import com.app.service.guardian.dto.GuardianDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class GuardianService {

    @Autowired
    private GuardianMapper mapper;

    @Autowired
    private GuardianChildService guardianChildService;

    public Optional<Guardian> findById(String id) {
        return Optional.ofNullable(mapper.selectById(id));
    }

    public Optional<Guardian> findByUnionId(String unionId) {
        return Optional.ofNullable(mapper.selectOne(new QueryWrapper<Guardian>().lambda()
                .eq(Guardian::getUnionId, unionId)
        ));
    }

    public Optional<Guardian> findByOpenId(String openId) {
        return Optional.ofNullable(mapper.selectOne(new QueryWrapper<Guardian>().lambda()
                .eq(Guardian::getOpenId, openId)
        ));
    }

    public Guardian saveOrUpdate(Guardian account) {
        Guardian _guardian;
        if (StringUtils.isEmpty(account.getId())) {
            _guardian = new Guardian();
            _guardian.setNickname(account.getNickname());
            _guardian.setAvatarUrl(account.getAvatarUrl());
            _guardian.setGender(account.getGender());
            mapper.insert(_guardian);
        } else {
            _guardian = findById(account.getId()).get();
            _guardian.setNickname(account.getNickname());
            _guardian.setAvatarUrl(account.getAvatarUrl());
            _guardian.setGender(account.getGender());
            mapper.updateById(_guardian);
        }

        return _guardian;
    }

    public GuardianDto find2Dto(String guardianId) {
        Optional<Guardian> optional = findById(guardianId);
        if (optional.isPresent()) {
            Guardian account = optional.get();
            GuardianDto dto = new GuardianDto();
            BeanUtils.copyProperties(account, dto);

            dto.setGuardianChildList(guardianChildService.findByGuardianId(guardianId));
            return dto;
        }

        return null;
    }

    /**
     * 功能描述：账号分页查询
     *
     * @return
     */
    public PageResult<GuardianDto> page(SearchAccountDto params) {
        List<GuardianDto> accountList = new ArrayList<>();
        IPage<Guardian> list = mapper.page(params, new Page(params.getPage(), params.getSize()));

        for (Guardian _guardian : list.getRecords()) {
            GuardianDto dto = new GuardianDto();
            BeanUtils.copyProperties(_guardian, dto);
            dto.setGuardianChildList(guardianChildService.findByGuardianId(_guardian.getId()));
            accountList.add(dto);
        }

        return new PageResult(params.getPage(), params.getSize(), list.getTotal(), accountList);
    }

}
