package com.zcloud.service.hiddenDangerCheckStandard.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.dsno2.hiddenDangerCheckStandard.CommonItemMapper;
import com.zcloud.service.hiddenDangerCheckStandard.CommonItemService;
import com.zcloud.service.hiddenDangerCheckStandard.CommonService;
import com.zcloud.service.hiddenDangerCheckStandard.DictionaryService;
import com.zcloud.util.UuidUtil;
import com.zcloud.util.Warden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 说明：隐患排查标准明细表（通用）
 * 作者：wangpeng
 * 时间：2024-01-25
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CommonItemServiceImpl implements CommonItemService {

    @Resource
    private CommonItemMapper commonItemMapper;

    @Autowired
    private CommonService commonService;
    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 新增
     *
     * @param pd
     * @throws Exception
     */
    public void save(PageData pd) throws Exception {
        commonItemMapper.save(pd);
    }

    /**新增
     * @param pd
     * @throws Exception
     */
    public void saveItem(PageData pd)throws Exception {
        if ("value".equals(pd.getString("CHECK_CATEGORY_WAY"))) {
            // 标准类别
            PageData commonDict1 = new PageData();
            commonDict1.put("DICTIONARY_ID", UuidUtil.get32UUID());
            commonDict1.put("DICTIONARY_NAME", pd.getString("CHECK_CATEGORY"));
            commonDict1.put("PARENT_ID", "0");
            commonDict1.put("PARENT_IDS", commonDict1.getString("DICTIONARY_ID"));
            commonDict1.put("DICTIONARY_NAMES", commonDict1.getString("DICTIONARY_NAME"));
            commonDict1.put("DICTIONARY_LEVEL", 1);
            Warden.initDate(commonDict1);
            Integer count = dictionaryService.getCount(commonDict1);
            commonDict1.put("DICTIONARY_SORT", count+1);
            commonDict1.put("DICTIONARY_SOURCE", 1);
            dictionaryService.save(commonDict1);
            pd.put("CHECK_CATEGORY", commonDict1.getString("DICTIONARY_ID"));

            // 标准项目
            PageData commonDict2 = new PageData();
            commonDict2.put("DICTIONARY_ID", UuidUtil.get32UUID());
            commonDict2.put("DICTIONARY_NAME", pd.getString("CHECK_ITEM"));
            commonDict2.put("PARENT_ID", commonDict1.getString("DICTIONARY_ID"));
            commonDict2.put("PARENT_IDS", commonDict1.getString("DICTIONARY_ID")+","+commonDict2.getString("DICTIONARY_ID"));
            commonDict2.put("DICTIONARY_NAMES", commonDict1.getString("DICTIONARY_NAME")+","+commonDict2.getString("DICTIONARY_NAME"));
            commonDict2.put("DICTIONARY_LEVEL", 2);
            Warden.initDate(commonDict2);
            count = dictionaryService.getCount(commonDict2);
            commonDict2.put("DICTIONARY_SORT", count+1);
            commonDict2.put("DICTIONARY_SOURCE", 1);
            dictionaryService.save(commonDict2);
            pd.put("CHECK_ITEM", commonDict2.getString("DICTIONARY_ID"));
        }
        commonItemMapper.save(pd);
    }

    /**
     * 删除
     *
     * @param pd
     * @throws Exception
     */
    public void delete(PageData pd) throws Exception {
        commonItemMapper.delete(pd);
    }

    /**
     * 修改
     *
     * @param pd
     * @throws Exception
     */
    public void edit(PageData pd) throws Exception {
        Warden.updateDate(pd);
        commonItemMapper.edit(pd);
    }

    /**
     * 修改
     *
     * @param pd
     * @throws Exception
     */
    public void editItem(PageData pd) throws Exception {
        if ("value".equals(pd.getString("CHECK_CATEGORY_WAY"))) {
            // 标准类别
            PageData commonDict1 = new PageData();
            commonDict1.put("DICTIONARY_ID", UuidUtil.get32UUID());
            commonDict1.put("DICTIONARY_NAME", pd.getString("CHECK_CATEGORY"));
            commonDict1.put("PARENT_ID", "0");
            commonDict1.put("PARENT_IDS", commonDict1.getString("DICTIONARY_ID"));
            commonDict1.put("DICTIONARY_NAMES", commonDict1.getString("DICTIONARY_NAME"));
            commonDict1.put("DICTIONARY_LEVEL", 1);
            Warden.initDate(commonDict1);
            Integer count = dictionaryService.getCount(commonDict1);
            commonDict1.put("DICTIONARY_SORT", count+1);
            commonDict1.put("DICTIONARY_SOURCE", 1);
            dictionaryService.save(commonDict1);
            pd.put("CHECK_CATEGORY", commonDict1.getString("DICTIONARY_ID"));

            // 标准项目
            PageData commonDict2 = new PageData();
            commonDict2.put("DICTIONARY_ID", UuidUtil.get32UUID());
            commonDict2.put("DICTIONARY_NAME", pd.getString("CHECK_ITEM"));
            commonDict2.put("PARENT_ID", commonDict1.getString("DICTIONARY_ID"));
            commonDict2.put("PARENT_IDS", commonDict1.getString("DICTIONARY_ID")+","+commonDict2.getString("DICTIONARY_ID"));
            commonDict2.put("DICTIONARY_NAMES", commonDict1.getString("DICTIONARY_NAME")+","+commonDict2.getString("DICTIONARY_NAME"));
            commonDict2.put("DICTIONARY_LEVEL", 2);
            Warden.initDate(commonDict2);
            count = dictionaryService.getCount(commonDict2);
            commonDict2.put("DICTIONARY_SORT", count+1);
            commonDict2.put("DICTIONARY_SOURCE", 1);
            dictionaryService.save(commonDict2);
            pd.put("CHECK_ITEM", commonDict2.getString("DICTIONARY_ID"));
        }
        commonItemMapper.edit(pd);
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    public List<PageData> list(Page page) throws Exception {
        return commonItemMapper.datalistPage(page);
    }

    /**
     * 列表(全部)
     *
     * @param pd
     * @throws Exception
     */
    public List<PageData> listAll(PageData pd) throws Exception {
        return commonItemMapper.listAll(pd);
    }

    /**
     * 通过id获取数据
     *
     * @param pd
     * @throws Exception
     */
    public PageData findById(PageData pd) throws Exception {
        return commonItemMapper.findById(pd);
    }

    /**
     * 批量删除
     *
     * @param pd
     * @throws Exception
     */
    public void deleteAll(PageData pd) throws Exception {
        commonItemMapper.deleteAll(pd);
    }

    /**通过id获取数据
     * @param pd
     * @throws Exception
     */
    @Override
    public Integer getCount(PageData pd)throws Exception {
        return commonItemMapper.getCount(pd);
    }

    /**删除(根据标准删除所有标准项)
     * @param pd
     * @throws Exception
     */
    @Override
    public void deleteByCommon(PageData pd)throws Exception {
        commonItemMapper.deleteByCommon(pd);
    }

}

