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

import cn.com.tenlion.systembase.dao.publicvideo.IPublicVideoDao;
import cn.com.tenlion.systembase.pojo.bos.publicvideo.PublicVideoBO;
import cn.com.tenlion.systembase.pojo.dtos.grid.GridDTO;
import cn.com.tenlion.systembase.pojo.dtos.publicvideo.PublicVideoDTO;
import cn.com.tenlion.systembase.pojo.pos.publicvideo.PublicVideoPO;
import cn.com.tenlion.systembase.pojo.vos.publicvideo.PublicVideoVO;
import cn.com.tenlion.systembase.service.grid.IGridService;
import cn.com.tenlion.systembase.service.publicvideo.IPublicVideoService;
import cn.com.tenlion.systembase.util.VerifyUtil;
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: PublicVideoServiceImpl
 * @Description: 城乡社区公共安全视频监控
 * @Author: CodeFactory
 * @Date: 2021-10-13 16:53:02
 * @Version: 3.0
 **/
@Service
public class PublicVideoServiceImpl extends DefaultBaseService implements IPublicVideoService {

    @Autowired
    private IPublicVideoDao publicVideoDao;
    @Autowired
    private IGridService gridService;

    @Override
    public void save(PublicVideoVO publicVideoVO) {
        saveReturnId(publicVideoVO);
    }

    @Override
    public void save(String token, PublicVideoVO publicVideoVO) {
        saveReturnId(token, publicVideoVO);
    }

    @Override
    public String saveReturnId(PublicVideoVO publicVideoVO) {
        return saveReturnId(null, publicVideoVO);
    }

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

    @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("publicVideoIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        publicVideoDao.remove(params);
    }

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

    @Override
    public void update(String publicVideoId, PublicVideoVO publicVideoVO) {
        update(null, publicVideoId, publicVideoVO);
    }

    @Override
    public void update(String token, String publicVideoId, PublicVideoVO publicVideoVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(publicVideoVO);
        params.put("publicVideoId", publicVideoId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        publicVideoDao.update(params);
    }

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

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

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

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

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

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

    @Override
    public List<PublicVideoDTO> list(Map<String, Object> params) {
        if(params.get("areaCodeLike") != null && !"".equals(params.get("areaCodeLike").toString())){
            String areaCoed = params.get("areaCodeLike").toString();
            String[] areaCodeSplit = VerifyUtil.splitStringByLength(areaCoed, 2);
            String areaCodeLike = "";
            for(int i = 5 ; i >= 0; i--){
                if(!"00".equals(areaCodeSplit[i])){
                    areaCodeLike = areaCoed.substring(0, (i + 1) * 2 );
                    break;
                }
            }
            params.put("areaCodeLike",areaCodeLike);
        }
        return publicVideoDao.list(params);
    }

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

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

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

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

}