package com.djhk.uav.policeWarLawLibrary.service.impl;


import com.djhk.common.core.domain.entity.SysDictData;
import com.djhk.common.utils.DateUtils;
import com.djhk.common.utils.StringUtils;
import com.djhk.system.service.impl.SysDictDataServiceImpl;
import com.djhk.uav.policeWarLawLibrary.domain.*;
import com.djhk.uav.policeWarLawLibrary.mapper.PoliceWarLawLibraryMapper;
import com.djhk.uav.policeWarLawLibrary.service.IPoliceWarLawLibraryService;
import com.djhk.uav.utils.ExportWord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 战法库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-21
 */
@Service
public class PoliceWarLawLibraryServiceImpl implements IPoliceWarLawLibraryService
{
    @Autowired
    private PoliceWarLawLibraryMapper policeWarLawLibraryMapper;

    @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private SysDictDataServiceImpl sysDictDataService;

    /**
     * 查询战法库
     * 
     * @param id 战法库主键
     * @return 战法库
     */
    @Override
    public PoliceWarLawLibrary selectPoliceWarLawLibraryById(Long id)
    {
        return policeWarLawLibraryMapper.selectPoliceWarLawLibraryById(id);
    }

    /**
     * 查询战法库列表
     * 
     * @param policeWarLawLibrary 战法库
     * @return 战法库
     */

    @Override
    public List<warNameVo> warNameList(PoliceWarLawLibrary policeWarLawLibrary)
    {
        return  policeWarLawLibraryMapper.warNameList(policeWarLawLibrary);
    }

    @Override
    public int removeByWarName(String warName) {
        return policeWarLawLibraryMapper.removeByWarName(warName);
    }

    @Override
    public void exprotByTenplete(HttpServletResponse response, PoliceWarLawLibrary policeWarLawLibrary) throws Exception {

        List<PoliceWarLawLibrary> policeWarLawLibraries = policeWarLawLibraryMapper.selectPoliceWarLawLibraryList(policeWarLawLibrary);
        String str = "";
        String remark = "";
        String process = "";
        String components = "";
        for (PoliceWarLawLibrary warLawLibrary : policeWarLawLibraries) {
            if (StringUtils.equals("node-2",warLawLibrary.getNodeName()))
            {
                str += warLawLibrary.getSonNodeData() + "\n";
                if (StringUtils.isNotEmpty(warLawLibrary.getRemark()))
                    remark += warLawLibrary.getRemark() + "\n";

            }
            if (StringUtils.equals("node-3",warLawLibrary.getNodeName()))
                components += warLawLibrary.getSonNodeData() + "\n";
            if (StringUtils.equals("node-4",warLawLibrary.getNodeName()))
                process += warLawLibrary.getSonNodeData() + ",";
        }
        if(process.length() > 0 )
            process = process.substring(0, process.length() - 1);
        Map<String, Object> dataMap = new HashMap<>();
        // 这里的key要与FTL模板中设置的${xxx}的值对应。
        dataMap.put("persons", str);
        dataMap.put("remark", remark);
        dataMap.put("process", process);
        dataMap.put("components", components);
        new ExportWord("UTF-8").exportDoc(response, "战法库", "planBook.ftl", dataMap, freeMarkerConfigurer);

    }
    @Override
    public HashMap<String, Object> selectPoliceWarLawLibraryList(PoliceWarLawLibrary policeWarLawLibrary)
    {
        List<PoliceWarLawLibrary> policeWarLawLibraries = policeWarLawLibraryMapper.selectPoliceWarLawLibraryList(policeWarLawLibrary);
        /**
         *  nodes: [
         *     { id: 'node-1', data: { label: '警用无人机挂网捕机技战法',}},
         *     { id: 'node-2',data: { label: '人员构成',}},
         *     { id: 'node-3',data: { label: '物资装备',}},
         *     { id: 'node-4',data: { label: '处置一般流程',}},
         *     { id: 'node-5',data: { label: '注意事项',}},
         *
         *     // { id: 'node-7',data: { label: 'b',} },
         *   ],
         */
        List<PoliceWarLawLibraryNodeDataDto> NodeDataList = new ArrayList<>();
        List<edgesListDto> edgesList = new ArrayList<>();

        for (PoliceWarLawLibrary warLawLibrary : policeWarLawLibraries) {
            PoliceWarLawLibraryNodeDataDto policeWarLawLibraryNodeDataDto = new PoliceWarLawLibraryNodeDataDto();
            edgesListDto edgesListDto = new edgesListDto();
            NodeDatas nodeDatas = new NodeDatas();
            nodeDatas.setLabel(warLawLibrary.getSonNodeData());
            policeWarLawLibraryNodeDataDto.setId("node" + warLawLibrary.getId());
            policeWarLawLibraryNodeDataDto.setData(nodeDatas);

            edgesListDto.setSource(warLawLibrary.getNodeName());
            edgesListDto.setTarget("node" + warLawLibrary.getId());

            NodeDataList.add(policeWarLawLibraryNodeDataDto);
            edgesList.add(edgesListDto);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("NodeDataList",NodeDataList);
        map.put("edgesList",edgesList);
        return map;
    }

    /**
     * 新增战法库
     * 
     * @return 结果
     */
    @Override
    public int insertPoliceWarLawLibrary(PoliceWarLawLibraryAddVO policeWarLawLibraryAddVO)
    {

        List<String> policeTypeSelectList = policeWarLawLibraryAddVO.getPoliceTypeSelectList();
        List<List<String>> policeTypeList = policeWarLawLibraryAddVO.getPoliceTypeList();
        List<String> equipmentList = policeWarLawLibraryAddVO.getEquipment();
        List<String> processList = policeWarLawLibraryAddVO.getProcessList();
        List<PoliceWarLawLibrary> out = new ArrayList<>();
//        List<SysDictData> sysDictDataList = sysDictDataService.getDictDataByType("sys_police_penson_type").getData();
        SysDictData sysDictData1 = new SysDictData();
        sysDictData1.setDictType("sys_police_penson_type");
        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(sysDictData1);

        for (int i = 0; i < policeTypeSelectList.size(); i++) {
            PoliceWarLawLibrary policeWarLawLibrary = new PoliceWarLawLibrary();
            policeWarLawLibrary.setSonNodeData(policeTypeSelectList.get(i));
            policeWarLawLibrary.setNodeName("node-2");
            policeWarLawLibrary.setWarName(policeWarLawLibraryAddVO.getWarName());
            policeWarLawLibrary.setCreateTime(DateUtils.getNowDate());
            policeWarLawLibrary.setDeptId(policeWarLawLibraryAddVO.getDeptId());
            if (sysDictDataList != null) {
                for (SysDictData sysDictData : sysDictDataList) {
                    if (StringUtils.equals(sysDictData.getDictLabel(),policeTypeSelectList.get(i)))
                    {
                        policeWarLawLibrary.setRemark(sysDictData.getDictLabel() + ":" + sysDictData.getRemark() + "\n");
                    }
                }

            }
            List<String> strList = policeTypeList.get(i);
            for (String str : strList) {
                policeWarLawLibrary.setSonNodeData(policeWarLawLibrary.getSonNodeData() + str + "\n");
            }
            out.add(policeWarLawLibrary);
        }

        for (String str : equipmentList) {
            if (StringUtils.isNotEmpty(str))
            {
                PoliceWarLawLibrary policeWarLawLibrary = new PoliceWarLawLibrary();
                policeWarLawLibrary.setSonNodeData(str);
                policeWarLawLibrary.setNodeName("node-3");
                policeWarLawLibrary.setWarName(policeWarLawLibraryAddVO.getWarName());
                policeWarLawLibrary.setCreateTime(DateUtils.getNowDate());
                policeWarLawLibrary.setDeptId(policeWarLawLibraryAddVO.getDeptId());
                out.add(policeWarLawLibrary);
            }

        }
        if (StringUtils.isNotEmpty(policeWarLawLibraryAddVO.getComponents()))
        {
            PoliceWarLawLibrary components = new PoliceWarLawLibrary();
            components.setSonNodeData(policeWarLawLibraryAddVO.getComponents());
            components.setNodeName("node-3");
            components.setWarName(policeWarLawLibraryAddVO.getWarName());
            components.setCreateTime(DateUtils.getNowDate());
            components.setDeptId(policeWarLawLibraryAddVO.getDeptId());
            out.add(components);
        }


        for (String str : processList) {
            if (StringUtils.isNotEmpty(str))
            {
                PoliceWarLawLibrary policeWarLawLibrary = new PoliceWarLawLibrary();
                policeWarLawLibrary.setSonNodeData(str);
                policeWarLawLibrary.setNodeName("node-4");
                policeWarLawLibrary.setWarName(policeWarLawLibraryAddVO.getWarName());
                policeWarLawLibrary.setCreateTime(DateUtils.getNowDate());
                policeWarLawLibrary.setDeptId(policeWarLawLibraryAddVO.getDeptId());
                out.add(policeWarLawLibrary);
            }

        }
        if (StringUtils.isNotEmpty(policeWarLawLibraryAddVO.getAttention()))
        {
            PoliceWarLawLibrary attention = new PoliceWarLawLibrary();
            attention.setSonNodeData(policeWarLawLibraryAddVO.getAttention());
            attention.setNodeName("node-5");
            attention.setWarName(policeWarLawLibraryAddVO.getWarName());
            attention.setCreateTime(DateUtils.getNowDate());
            attention.setDeptId(policeWarLawLibraryAddVO.getDeptId());
            out.add(attention);
        }



        for (PoliceWarLawLibrary policeWarLawLibrary : out) {

            policeWarLawLibraryMapper.insertPoliceWarLawLibrary(policeWarLawLibrary);
        }

        return out.size();
    }

    /**
     * 修改战法库
     * 
     * @param policeWarLawLibrary 战法库
     * @return 结果
     */
    @Override
    public int updatePoliceWarLawLibrary(PoliceWarLawLibrary policeWarLawLibrary)
    {
        policeWarLawLibrary.setUpdateTime(DateUtils.getNowDate());
        return policeWarLawLibraryMapper.updatePoliceWarLawLibrary(policeWarLawLibrary);
    }

    /**
     * 批量删除战法库
     * 
     * @param ids 需要删除的战法库主键
     * @return 结果
     */
    @Override
    public int deletePoliceWarLawLibraryByIds(Long[] ids)
    {
        return policeWarLawLibraryMapper.deletePoliceWarLawLibraryByIds(ids);
    }

    /**
     * 删除战法库信息
     * 
     * @param id 战法库主键
     * @return 结果
     */
    @Override
    public int deletePoliceWarLawLibraryById(Long id)
    {
        return policeWarLawLibraryMapper.deletePoliceWarLawLibraryById(id);
    }
}
