package com.jpsoft.shinestar.modules.base.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.CompanyGoodInfoDAO;
import com.jpsoft.shinestar.modules.base.dao.GoodsInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.CompanyGoodInfo;
import com.jpsoft.shinestar.modules.base.entity.GoodsInfo;
import com.jpsoft.shinestar.modules.base.service.GoodsInfoService;

import com.jpsoft.shinestar.modules.common.dto.Sort;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 墨鱼_mo
 * @date 2020-7-6 9:25
 */
@Transactional
@Component(value="goodsInfoService")
public class GoodsInfoServiceImpl implements GoodsInfoService {

    @Resource(name="goodsInfoDAO")
    private GoodsInfoDAO goodsInfoDAO;

    @Resource(name= "companyGoodInfoDAO")
    private CompanyGoodInfoDAO companyGoodInfoDAO;


    @Override
    public GoodsInfo get(String id) {
        return goodsInfoDAO.get(id);
    }

    @Override
    public boolean exist(String id) {
        int count = goodsInfoDAO.exist(id);

        return count > 0 ? true : false;
    }

    @Override
    public int insert(GoodsInfo model) {
        return goodsInfoDAO.insert(model);
    }

    @Override
    public int insertPayService(GoodsInfo model,String companyId) {
        int count = goodsInfoDAO.insert(model);
        int affectCount = 0;
        if(count > 0){
            CompanyGoodInfo companyGoodInfo = new CompanyGoodInfo();
            companyGoodInfo.setId(UUID.randomUUID().toString());
            companyGoodInfo.setCompanyId(companyId);
            companyGoodInfo.setCreateBy(model.getCreateBy());
            companyGoodInfo.setCreateTime(model.getCreateTime());
            companyGoodInfo.setDelFlag(false);
            companyGoodInfo.setGoodsId(model.getId());

            affectCount = companyGoodInfoDAO.insert(companyGoodInfo);
        }

        return affectCount;
    }

    @Override
    public int update(GoodsInfo model) {
        return goodsInfoDAO.update(model);
    }

    @Override
    public int delete(String id) {
        return goodsInfoDAO.delete(id);
    }

    @Override
    public List<GoodsInfo> list() {
        return goodsInfoDAO.list();
    }

    @Override
    public List<GoodsInfo> findByType(String type) {
        return goodsInfoDAO.findByType(type);
    }

    @Override
    public List<String> findBySearch(Map<String, Object> searchParams){
        return goodsInfoDAO.findBySearch(searchParams);
    }


    @Override
    public Page<GoodsInfo> pageSearch(Map<String, Object> searchParams, int pageNum, int pageSize, boolean count, List<Sort> sortList) {
        Page<GoodsInfo> page = PageHelper.startPage(pageNum,pageSize,count).doSelectPage(()->{
            goodsInfoDAO.search(searchParams,sortList);
        });

        return page;
    }

}
