package cn.com.tenlion.systembase.service.line.impl;

import cn.com.tenlion.systembase.dao.line.ILineDao;
import cn.com.tenlion.systembase.pojo.bos.line.LineBO;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.line.LineDTO;
import cn.com.tenlion.systembase.pojo.pos.line.LinePO;
import cn.com.tenlion.systembase.pojo.vos.line.LineVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.line.ILineService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: LineServiceImpl
 * @Description: 护路护线
 * @Author: CodeFactory
 * @Date: 2021-10-13 11:32:20
 * @Version: 3.0
 **/
@Service
public class LineServiceImpl extends DefaultBaseService implements ILineService {

    @Autowired
    private ILineDao lineDao;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(LineVO lineVO) {
        saveReturnId(lineVO);
    }

    @Override
    public void save(String token, LineVO lineVO) {
        saveReturnId(token, lineVO);
    }

    @Override
    public String saveReturnId(LineVO lineVO) {
        return saveReturnId(null, lineVO);
    }

    @Override
    public String saveReturnId(String token, LineVO lineVO) {
        String lineId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(lineVO);
        params.put("lineId", lineId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        lineDao.save(params);
        return lineId;
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("lineIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        lineDao.remove(params);
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("lineIds", ids);
        lineDao.delete(params);
    }

    @Override
    public void update(String lineId, LineVO lineVO) {
        update(null, lineId, lineVO);
    }

    @Override
    public void update(String token, String lineId, LineVO lineVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(lineVO);
        params.put("lineId", lineId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        lineDao.update(params);
    }

    @Override
    public LineDTO get(Map<String, Object> params) {
        LineDTO lineDTO = lineDao.get(params);
        if(null != lineDTO) {
            GridDTO gridDTO = gridService.get(lineDTO.getGrid());
            if(null != gridDTO) {
                lineDTO.setGridName(gridDTO.getGridName());
            }
        }
        return lineDTO;
    }

    @Override
    public LineDTO get(String lineId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("lineId", lineId);
        return get(params);
    }

    @Override
    public LineBO getBO(Map<String, Object> params) {
        return lineDao.getBO(params);
    }

    @Override
    public LineBO getBO(String lineId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("lineId", lineId);
        return getBO(params);
    }

    @Override
    public LinePO getPO(Map<String, Object> params) {
        return lineDao.getPO(params);
    }

    @Override
    public LinePO getPO(String lineId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("lineId", lineId);
        return getPO(params);
    }

    @Override
    public List<LineDTO> list(Map<String, Object> params) {
        return lineDao.list(params);
    }

    @Override
    public List<LineBO> listBO(Map<String, Object> params) {
        return lineDao.listBO(params);
    }

    @Override
    public List<LinePO> listPO(Map<String, Object> params) {
        return lineDao.listPO(params);
    }

    @Override
    public SuccessResultList<List<LineDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<LineDTO> lineDTOs = list(page.getParams());
        PageInfo<LineDTO> pageInfo = new PageInfo<>(lineDTOs);
        return new SuccessResultList<>(lineDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = lineDao.count(params);
        return count == null ? 0 : count;
    }

}