package cn.superdesk.uniorder.service._common.impl;


import cn.hutool.core.collection.CollUtil;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.common.UploadFiles;
import cn.superdesk.uniorder.dao.entity.common.UploadFilesExample;
import cn.superdesk.uniorder.dao.mapper.common.UploadFilesMapper;
import cn.superdesk.uniorder.dto.param._common.UploadFilesQueryCriteria;
import cn.superdesk.uniorder.service._common.UploadFilesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 文件上传信息
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-11-22 17:13:37
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-11-22 17:13:37
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class UploadFilesServiceImpl implements UploadFilesService {

    @Autowired
    private UploadFilesMapper uploadFilesMapper;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(UploadFiles entity) {
        return uploadFilesMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(UploadFiles entity) {
        return uploadFilesMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public UploadFiles findById(Long id) {
        return uploadFilesMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<UploadFiles> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<UploadFiles> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    public void addUploadFiles(UploadFiles entity) {
        uploadFilesMapper.insertSelective(entity);
    }

    public void updateUploadFiles(UploadFiles entity) {
        UploadFiles originEntity = uploadFilesMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        uploadFilesMapper.updateByPrimaryKey(originEntity);
    }

    public void deleteUploadFiles(Long id) {
        uploadFilesMapper.deleteByPrimaryKey(id);
    }

    public UploadFiles findUploadFilesById(Long id) {
        UploadFiles entity = uploadFilesMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

    public UploadFiles findUploadFilesByUid(String uid) {

        UploadFilesExample          example  = new UploadFilesExample();
        UploadFilesExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);

        List<UploadFiles> resultList = uploadFilesMapper.selectByExample(example);

        if (CollUtil.isNotEmpty(resultList)) {
            return resultList.get(0);
        }

        return null;
    }

    public List<UploadFiles> findUploadFilesByIds(List<Long> ids) {

        UploadFilesExample          example  = new UploadFilesExample();
        UploadFilesExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);

        List<UploadFiles> resultList = uploadFilesMapper.selectByExample(example);
        return resultList;
    }

    public List<UploadFiles> findUploadFilesByUids(List<String> uids) {

        UploadFilesExample          example  = new UploadFilesExample();
        UploadFilesExample.Criteria criteria = example.createCriteria();
        criteria.andUidIn(uids);

        List<UploadFiles> resultList = uploadFilesMapper.selectByExample(example);
        return resultList;
    }

    public R<List<UploadFiles>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        UploadFilesExample          example  = new UploadFilesExample();
        UploadFilesExample.Criteria criteria = example.createCriteria();

        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 UploadFilesExample.Criteria
        // UploadFiles uploadFilesConditions = BeanUtils.mapToBean(conditions,UploadFiles.class);

        PageHelper.startPage(pageNun, pageSize);
        List<UploadFiles>     resultList = uploadFilesMapper.selectByExample(example);
        PageInfo<UploadFiles> page       = new PageInfo<UploadFiles>(resultList);
        PageHelper.clearPage();

        return new R<List<UploadFiles>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    public R<List<UploadFiles>> pageQueryGenericParam(UploadFilesQueryCriteria queryCriteria) {

        UploadFilesExample          example  = new UploadFilesExample();
        UploadFilesExample.Criteria criteria = example.createCriteria();

        // TODO 添加条件... 后期添加 queryCriteria 转 UploadFilesExample.Criteria

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<UploadFiles>     resultList = uploadFilesMapper.selectByExample(example);
        PageInfo<UploadFiles> page       = new PageInfo<UploadFiles>(resultList);
        PageHelper.clearPage();

        return new R<List<UploadFiles>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

}