package com.inspur.service.impl;

import com.inspur.common.utils.DateUtils;
import com.inspur.common.utils.StringUtils;
import com.inspur.domain.ElevatorWorkNode;
import com.inspur.domain.ElevatorWorkOrder;
import com.inspur.domain.ElevatorWorkOrderExport;
import com.inspur.mapper.ElevatorWorkNodeMapper;
import com.inspur.mapper.ElevatorWorkOrderMapper;
import com.inspur.service.IElevatorWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.inspur.common.utils.PageUtils.startPage;

/**
 * 工单系统Service业务层处理
 *
 * @author inspur
 * @date 2024-09-20
 */
@Service
public class ElevatorWorkOrderServiceImpl implements IElevatorWorkOrderService
{
    @Autowired
    private ElevatorWorkOrderMapper elevatorWorkOrderMapper;

    @Autowired
    private ElevatorWorkNodeMapper elevatorWorkNodeMapper;

    private static final String BT_USER_REGION_CODE = "659000";

    /**
     * 查询工单系统
     *
     * @param id 工单系统主键
     * @return 工单系统
     */
    @Override
    public ElevatorWorkOrder selectElevatorWorkOrderById(Long id)
    {
        return elevatorWorkOrderMapper.selectElevatorWorkOrderById(id);
    }

    /**
     * 查询工单系统列表
     *
     * @param elevatorWorkOrder 工单系统
     * @return 工单系统
     */
    @Override
    public List<ElevatorWorkOrder> selectElevatorWorkOrderList(ElevatorWorkOrder elevatorWorkOrder) {
        List<String> areaCodes = Arrays.asList(elevatorWorkOrder.getUserRegionCode().split(","));
        if (!areaCodes.contains(BT_USER_REGION_CODE)) {
            elevatorWorkOrder.setRegionCodes(areaCodes);
        }
        startPage();
        List<ElevatorWorkOrder> list = elevatorWorkOrderMapper.selectElevatorWorkOrderList(elevatorWorkOrder);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(item -> {
                ElevatorWorkNode queryParam = new ElevatorWorkNode();
                queryParam.setWorkOrderId(item.getId());
                queryParam.setType("2");
                List<ElevatorWorkNode> workNodeList = elevatorWorkNodeMapper.selectElevatorWorkNodeList(queryParam);
                List<String> dtmfList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(workNodeList)) {
                    workNodeList.forEach(workNode -> {
                        String contentName = workNode.getContentName();
                        if (!StringUtils.isEmpty(contentName)) {
                            dtmfList.add(contentName.replace("电梯安全码：",""));
                        }
                    });
                }
                if (!CollectionUtils.isEmpty(dtmfList)){
                    // 逗号分割形成字符串
                    item.setDtmfList(String.join(",", dtmfList));
                }
            });
        }
        return list;
    }

    @Override
    public List<ElevatorWorkOrderExport> selectElevatorWorkOrderNoAnswer(ElevatorWorkOrder elevatorWorkOrder) {
        List<String> areaCodes = Arrays.asList(elevatorWorkOrder.getUserRegionCode().split(","));
        if (!areaCodes.contains(BT_USER_REGION_CODE)) {
            elevatorWorkOrder.setRegionCodes(areaCodes);
        }
        List<ElevatorWorkOrderExport> result = elevatorWorkOrderMapper.selectElevatorWorkOrderNoAnswer(elevatorWorkOrder);
        if (!CollectionUtils.isEmpty(result)) {
            result.forEach(item -> {
                ElevatorWorkNode queryParam = new ElevatorWorkNode();
                queryParam.setWorkOrderId(item.getId());
                queryParam.setType("2");
                List<ElevatorWorkNode> workNodeList = elevatorWorkNodeMapper.selectElevatorWorkNodeList(queryParam);
                List<String> dtmfList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(workNodeList)) {
                    workNodeList.forEach(workNode -> {
                        String contentName = workNode.getContentName();
                        if (!StringUtils.isEmpty(contentName)) {
                            dtmfList.add(contentName.replace("电梯安全码：",""));
                        }
                    });
                }
                if (!CollectionUtils.isEmpty(dtmfList)){
                    // 逗号分割形成字符串
                    item.setDtmfList(String.join(",", dtmfList));
                }
            });
        }
        return result;
    }

    /**
     * 新增工单系统
     *
     * @param elevatorWorkOrder 工单系统
     * @return 结果
     */
    @Override
    public int insertElevatorWorkOrder(ElevatorWorkOrder elevatorWorkOrder) {
        elevatorWorkOrder.setCreateTime(DateUtils.getNowDate());
        return elevatorWorkOrderMapper.insertElevatorWorkOrder(elevatorWorkOrder);
    }

    /**
     * 修改工单系统
     *
     * @param elevatorWorkOrder 工单系统
     * @return 结果
     */
    @Override
    public int updateElevatorWorkOrder(ElevatorWorkOrder elevatorWorkOrder)
    {
        elevatorWorkOrder.setUpdateTime(DateUtils.getNowDate());
        return elevatorWorkOrderMapper.updateElevatorWorkOrder(elevatorWorkOrder);
    }

    /**
     * 批量删除工单系统
     *
     * @param ids 需要删除的工单系统主键
     * @return 结果
     */
    @Override
    public int deleteElevatorWorkOrderByIds(Long[] ids)
    {
        return elevatorWorkOrderMapper.deleteElevatorWorkOrderByIds(ids);
    }

    /**
     * 删除工单系统信息
     *
     * @param id 工单系统主键
     * @return 结果
     */
    @Override
    public int deleteElevatorWorkOrderById(Long id)
    {
        return elevatorWorkOrderMapper.deleteElevatorWorkOrderById(id);
    }
}
