package com.yc.ship.module.resource.service.guide;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.guide.vo.GuidePageReqVO;
import com.yc.ship.module.resource.controller.admin.guide.vo.GuideSaveReqVO;
import com.yc.ship.module.resource.controller.admin.guide.vo.GuideSimpleRespVO;
import com.yc.ship.module.resource.dal.dataobject.guide.GuideDO;
import com.yc.ship.module.resource.dal.mysql.guide.GuideMapper;
import com.yc.ship.module.resource.service.guidegroup.GuideGroupService;
import com.yc.ship.module.system.api.user.AdminUserApi;
import com.yc.ship.module.system.api.user.dto.AdminUserRespDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.GUIDE_NOT_EXISTS;


/**
 * 导游管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class GuideServiceImpl implements GuideService {

    @Resource
    private GuideMapper guideMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private GuideGroupService guideGroupService;

    @Override
    public String createGuide(GuideSaveReqVO createReqVO) {
        // 创建系统用户
        AdminUserRespDTO adminDto = new AdminUserRespDTO();
        adminDto.setNickname(createReqVO.getName());
        adminDto.setUsername(PinyinUtil.getPinyin(createReqVO.getName(), ""));
        adminDto.setMobile(createReqVO.getPhone());
        adminDto.setPassword(createReqVO.getPhone());
        adminDto.setSex(createReqVO.getSex());
        Long userResult = adminUserApi.createUser(adminDto);
        // 插入
        GuideDO guide = BeanUtils.toBean(createReqVO, GuideDO.class);
        guide.setId(String.valueOf(userResult));
        guide.setDeleted(false);
        guideMapper.insert(guide);
        // 返回
        return guide.getId();
    }

    @Override
    public void updateGuide(GuideSaveReqVO updateReqVO) {
        // 校验存在
        validateGuideExists(updateReqVO.getId());
        // 更新
        GuideDO updateObj = BeanUtils.toBean(updateReqVO, GuideDO.class);
        guideMapper.updateById(updateObj);
        AdminUserRespDTO result = adminUserApi.getUser(Long.parseLong(updateObj.getId()));
        if (result == null) {
            // 创建系统用户
            AdminUserRespDTO adminDto = new AdminUserRespDTO();
            adminDto.setNickname(updateReqVO.getName());
            adminDto.setUsername(PinyinUtil.getPinyin(updateReqVO.getName(), ""));
            adminDto.setMobile(updateReqVO.getPhone());
            adminDto.setPassword(updateReqVO.getPhone());
            adminDto.setSex(updateReqVO.getSex());
            adminDto.setId(Long.parseLong(updateObj.getId()));
            adminUserApi.createUser(adminDto);
        }
    }

    @Override
    public void deleteGuide(String id) {
        // 校验存在
        validateGuideExists(id);
        // 删除系统用户信息
        adminUserApi.deleteUser(Long.valueOf(id));
        // 删除
        guideMapper.deleteById(id);
    }

    private void validateGuideExists(String id) {
        if (guideMapper.selectById(id) == null) {
            throw exception(GUIDE_NOT_EXISTS);
        }
    }

    @Override
    public GuideDO getGuide(String id) {
        return guideMapper.selectById(id);
    }

    @Override
    public PageResult<GuideDO> getGuidePage(GuidePageReqVO pageReqVO) {
        if (StringUtils.isNotBlank(pageReqVO.getGroupCode())) {
            List<String> groupAndAllChildren = guideGroupService.getGroupAndAllChildren(pageReqVO.getGroupCode());
            pageReqVO.setGroupList(groupAndAllChildren);
            pageReqVO.setGroupCode(null);
        }
        return guideMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GuideDO> getGuideList(GuideSimpleRespVO listReqVO) {
        List<GuideDO> list = guideMapper.selectList(listReqVO);
        list.sort(Comparator.comparing(GuideDO::getUpdateTime));
        return list;
    }

}
