package com.dgtis.dorm.manager.db.service;

import com.dgtis.dorm.manager.db.dao.AdministrativeItemsDocMapper;
import com.dgtis.dorm.manager.db.dao.AdministrativeItemsMapper;
import com.dgtis.dorm.manager.db.domain.AdministrativeItems;
import com.dgtis.dorm.manager.db.domain.AdministrativeItemsDoc;
import com.dgtis.dorm.manager.db.param.ItemsQueryParam;
import com.dgtis.dorm.manager.db.util.ArtificialUtil;
import com.dgtis.dorm.manager.db.util.ListToTreeUtil;
import com.dgtis.dorm.manager.db.util.TreeNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 办理事项
 *
 * @author huanghaoqi
 * @version 1.0
 * @date 2018年12月03日 15:27:29
 * @since JDK1.8
 */
@Service
public class AdministrativeItemsService {
    @Resource
    private AdministrativeItemsMapper administrativeItemsMapper;
    @Resource
    private AdministrativeItemsDocMapper administrativeItemsDocMapper;

    /**
     * 方法描述  查询列表
     *
     * @param queryParam 查询参数
     * @return List<AdministrativeItems>
     * @author huanghaoqi
     * @date 2018年12月03日 15:29:37
     */
    public List<AdministrativeItems> list(ItemsQueryParam queryParam) {

        return administrativeItemsMapper.selectList(queryParam);

    }

    /**
     * 方法描述  查询总数
     *
     * @param queryParam 查询参数
     * @return Long
     * @author huanghaoqi
     * @date 2018年12月03日 15:31:02
     */
    public Long count(ItemsQueryParam queryParam) {
        return administrativeItemsMapper.count(queryParam);

    }

    /**
     * 方法描述  添加
     *
     * @param administrativeItems 添加对象
     * @return Integer
     * @author huanghaoqi
     * @date 2018年12月03日 15:32:05
     */
    @Transactional
    public Integer insert(AdministrativeItems administrativeItems) {
        if(StringUtils.isNotBlank(administrativeItems.getName())){
            //设置拼音
            String pinyinName = ArtificialUtil.getPingYin(administrativeItems.getName());
            administrativeItems.setNameSpell(pinyinName.toUpperCase());
        }
        //设置等级
        Integer parentId = administrativeItems.getParentId();
        if (parentId == null) {
            administrativeItems.setLevel(1);
        } else {
            AdministrativeItems detail = administrativeItemsMapper.selectByPrimaryKey(parentId);
            if (detail != null) {
                administrativeItems.setLevel(detail.getLevel() + 1);
            } else {
                administrativeItems.setLevel(1);
            }
        }
        administrativeItemsMapper.insert(administrativeItems);
        Integer id = administrativeItems.getId();
        // 指南相关文档
        List<AdministrativeItemsDoc> itemsGuidanceDocList = administrativeItems.getItemsGuidanceDocList();
        if (!CollectionUtils.isEmpty(itemsGuidanceDocList)) {
            itemsGuidanceDocList.forEach(administrativeItemsDoc -> {
                administrativeItemsDoc.setItemsId(id);
                administrativeItemsDoc.setType(1);
                administrativeItemsDoc.setCreateDate(LocalDateTime.now());
                administrativeItemsDoc.setCreateUser(administrativeItems.getCreateUser());
            });

            administrativeItemsDocMapper.insertBatch(itemsGuidanceDocList);
        }
        // 表格相关文档
        List<AdministrativeItemsDoc> itemsTableDocList = administrativeItems.getItemsTableDocList();
        if (!CollectionUtils.isEmpty(itemsTableDocList)) {
            itemsTableDocList.forEach(administrativeItemsDoc -> {
                administrativeItemsDoc.setItemsId(id);
                administrativeItemsDoc.setType(2);
                administrativeItemsDoc.setCreateDate(LocalDateTime.now());
                administrativeItemsDoc.setCreateUser(administrativeItems.getCreateUser());
            });

            administrativeItemsDocMapper.insertBatch(itemsTableDocList);
        }
        return id;
    }

    /**
     * 方法描述  修改
     *
     * @param administrativeItems 修改对象
     * @return Integer
     * @author huanghaoqi
     * @date 2018年12月03日 15:32:05
     */
    @Transactional
    public Integer update(AdministrativeItems administrativeItems) {
        if(StringUtils.isNotBlank(administrativeItems.getName())){
            //设置拼音
            String pinyinName = ArtificialUtil.getPingYin(administrativeItems.getName());
            administrativeItems.setNameSpell(pinyinName.toUpperCase());
        }
        //设置等级
        Integer parentId = administrativeItems.getParentId();
        if (parentId == null) {
            administrativeItems.setLevel(1);
        } else {
            AdministrativeItems detail = administrativeItemsMapper.selectByPrimaryKey(parentId);
            if (detail != null) {
                administrativeItems.setLevel(detail.getLevel() + 1);
            } else {
                administrativeItems.setLevel(1);
            }
        }
        administrativeItemsMapper.update(administrativeItems);

        Integer id = administrativeItems.getId();
        // 先删除后插入
        administrativeItemsDocMapper.deleteByItemsId(id);
        // 指南相关文档
        List<AdministrativeItemsDoc> itemsGuidanceDocList = administrativeItems.getItemsGuidanceDocList();
        if (!CollectionUtils.isEmpty(itemsGuidanceDocList)) {
            itemsGuidanceDocList.forEach(administrativeItemsDoc -> {
                administrativeItemsDoc.setItemsId(id);
                administrativeItemsDoc.setType(1);
                administrativeItemsDoc.setCreateDate(LocalDateTime.now());
                administrativeItemsDoc.setCreateUser(administrativeItems.getCreateUser());
            });

            administrativeItemsDocMapper.insertBatch(itemsGuidanceDocList);
        }
        // 表格相关文档
        List<AdministrativeItemsDoc> itemsTableDocList = administrativeItems.getItemsTableDocList();
        if (!CollectionUtils.isEmpty(itemsTableDocList)) {
            itemsTableDocList.forEach(administrativeItemsDoc -> {
                administrativeItemsDoc.setItemsId(id);
                administrativeItemsDoc.setType(2);
                administrativeItemsDoc.setCreateDate(LocalDateTime.now());
                administrativeItemsDoc.setCreateUser(administrativeItems.getCreateUser());
            });

            administrativeItemsDocMapper.insertBatch(itemsTableDocList);
        }
        return id;
    }

    /**
     * 方法描述  添加二维码
     *
     * @param qrCodeStorageId 二维码ID
     * @return Integer
     * @author huanghaoqi
     * @date 2018年12月03日 15:32:05
     */
    public Integer addQrCode(Integer id, String qrCodeStorageId) {
        AdministrativeItems administrativeItems = new AdministrativeItems();
        administrativeItems.setId(id);
        administrativeItems.setQrcodeStorageId(qrCodeStorageId);
        administrativeItemsMapper.update(administrativeItems);
        return administrativeItems.getId();
    }


    /**
     * 方法描述  根据ID查询
     *
     * @param id ID
     * @return AdministrativeItems
     * @author huanghaoqi
     * @date 2018年12月03日 15:35:35
     */
    public AdministrativeItems findById(Integer id) {
        AdministrativeItems administrativeItems = administrativeItemsMapper.selectByPrimaryKey(id);
        List<AdministrativeItemsDoc> administrativeItemsDocList = administrativeItemsDocMapper.selectByItemsId(id, null);

        //办事指南文档列表
        List<AdministrativeItemsDoc> itemsGuidanceDocList = administrativeItemsDocList.stream().filter(administrativeItemsDoc -> 1 == administrativeItemsDoc.getType()).collect(Collectors.toList());
        //表格文档列表
        List<AdministrativeItemsDoc> itemsTableDocList = administrativeItemsDocList.stream().filter(administrativeItemsDoc -> 2 == administrativeItemsDoc.getType()).collect(Collectors.toList());
        administrativeItems.setItemsGuidanceDocList(itemsGuidanceDocList);
        administrativeItems.setItemsTableDocList(itemsTableDocList);
        return administrativeItems;
    }

    /**
     * 方法描述  批量删除
     *
     * @param ids id集合
     * @author huanghaoqi
     * @date 2018年12月03日 15:49:35
     */
    @Transactional
    public void batchDelete(List<Integer> ids) {
        administrativeItemsMapper.deleteByIds(ids);
        administrativeItemsDocMapper.deleteByItemsIds(ids);
    }

    /**
     * 方法描述  查询父类树结构
     *
     * @author huanghaoqi
     * @date 2018年12月04日 17:34:43
     */
    public List<TreeNode> getParentTree() {
        List<AdministrativeItems> administrativeItemsList = administrativeItemsMapper.selectList(new ItemsQueryParam());
        if (CollectionUtils.isEmpty(administrativeItemsList)) {
            return new ArrayList<>(0);
        }
        List<TreeNode> treeNodes = new ArrayList<>();
        administrativeItemsList.forEach(item -> {
            TreeNode treeNode = new TreeNode(item.getId(), item.getName(), item.getParentId(), item.getLevel());
            treeNodes.add(treeNode);
        });

        return ListToTreeUtil.bulid(treeNodes);
    }

    /**
     * 方法描述  根据ID集合查询
     *
     * @author huanghaoqi
     * @date 2018年12月03日 15:14:13
     */
    public List<AdministrativeItems> selectByIds(List<Integer> ids) {
        return administrativeItemsMapper.selectByIds(ids);
    }

    /**
     * 方法描述  根据事项ID查询
     *
     * @author huanghaoqi
     * @date 2018年12月03日 15:09:14
     */
    public List<AdministrativeItemsDoc> selectByItemsId(Integer itemsId, Integer type) {
        return administrativeItemsDocMapper.selectByItemsId(itemsId, type);
    }

    /**
     * 方法描述  根据一级name+二级name查询二维码图片
     * @param oneTitlePY
     * @param twoTitlePY
     * @return
     */
    public List<String> findQrcodeStorageUrlByNames(String oneTitlePY, String twoTitlePY) {
        return administrativeItemsMapper.selectQrcodeStorageUrlByNames(oneTitlePY, twoTitlePY);
    }
}
