package com.hwrj.cloud.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.admin.dao.UmsMemberOriginProductDao;
import com.hwrj.cloud.admin.dto.UmsMemberOriginProductParam;
import com.hwrj.cloud.admin.mapper.UmsMemberOriginProductMapper;
import com.hwrj.cloud.admin.model.UmsMemberOriginProduct;
import com.hwrj.cloud.admin.model.UmsMemberOriginProductExample;
import com.hwrj.cloud.admin.service.UmsMemberOriginProductService;
import com.hwrj.cloud.admin.util.CurrUserUtil;
import com.hwrj.cloud.common.exception.GlobalException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @className:
 * @description:
 * @author: lym
 * @date: 2020/9/7 10:24
 */
@Service
public class UmsMemberOriginProductServiceImpl implements UmsMemberOriginProductService {

    @Resource
    private UmsMemberOriginProductMapper umsMemberOriginProductMapper;
    @Resource
    private UmsMemberOriginProductDao umsMemberOriginProductDao;

    @Override
    public List<UmsMemberOriginProduct> list(UmsMemberOriginProductParam originProduct) {
        PageHelper.startPage(originProduct.getPageNum(), originProduct.getPageSize());
        UmsMemberOriginProductExample example = new UmsMemberOriginProductExample();
        UmsMemberOriginProductExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(originProduct.getFkOriginId())) {
            criteria.andFkOriginIdNotEqualTo(originProduct.getFkOriginId());
        }
        if (!StringUtils.isEmpty(originProduct.getProductName())) {
            criteria.andProductNameLike(originProduct.getProductName());
        }
        if (!StringUtils.isEmpty(originProduct.getPlantArea())) {
            criteria.andPlantAreaLike(originProduct.getPlantArea());
        }
        if (!StringUtils.isEmpty(originProduct.getOutputNum())) {
            criteria.andOutputNumLike(originProduct.getOutputNum());
        }
        if (!StringUtils.isEmpty(originProduct.getOutputValue())) {
            criteria.andOutputValueLike(originProduct.getOutputValue());
        }
        criteria.andStateEqualTo(0);
        example.setOrderByClause("create_time desc");
        return umsMemberOriginProductMapper.selectByExample(example);
    }

    @Override
    public int save(UmsMemberOriginProductParam originProduct) {
        isExist(originProduct); //检验唯一

        UmsMemberOriginProduct product = new UmsMemberOriginProduct();
        BeanUtils.copyProperties(originProduct,product);
        product.setState(0);
        product.setCreateId(CurrUserUtil.getUserId());
        product.setCreateTime(new Date());
        return umsMemberOriginProductMapper.insertSelective(product);
    }

    @Override
    public int update(UmsMemberOriginProductParam originProduct) {
        isExist(originProduct); //检验唯一

        UmsMemberOriginProduct product = new UmsMemberOriginProduct();
        BeanUtils.copyProperties(originProduct,product);
        product.setUpdateId(CurrUserUtil.getUserId());
        product.setUpdateTime(new Date());
        return umsMemberOriginProductMapper.updateByPrimaryKeySelective(product);
    }

    /**
     * 检验唯一
     * lym
     * @param originProduct
     */
    private void isExist(UmsMemberOriginProductParam originProduct){
        UmsMemberOriginProductExample example = new UmsMemberOriginProductExample();
        UmsMemberOriginProductExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(originProduct.getId())){
            criteria.andIdNotEqualTo(originProduct.getId());
        }
        criteria.andFkOriginIdEqualTo(originProduct.getFkOriginId());
        criteria.andProductNameEqualTo(originProduct.getProductName());
        criteria.andStateEqualTo(0);
        long i = umsMemberOriginProductMapper.countByExample(example);
        if (i > 0){
            throw new GlobalException(500,"产出物已存在!");
        }
    }

    @Override
    public int delete(long id) {
        UmsMemberOriginProduct product = new UmsMemberOriginProduct();
        product.setId(id);
        product.setState(2);
        return umsMemberOriginProductMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public int saveOrUpdate(List<UmsMemberOriginProduct> list, long originId) {
        List<UmsMemberOriginProduct> saveList = new ArrayList<>(),updateList = new ArrayList<>();
        List<Long> ids = new ArrayList<>();
        for (UmsMemberOriginProduct product:list){
            product.setFkOriginId(originId);
            if (StringUtils.isEmpty(product.getId())){
                isExistOriginProduct(product);
                product.setCreateId(CurrUserUtil.getUserId());
                product.setState(0);
                saveList.add(product);
            }else {
                isExistOriginProduct(product);
                ids.add(product.getId());
                product.setUpdateId(CurrUserUtil.getUserId());
                updateList.add(product);
            }
        }

        //删除
        int i = umsMemberOriginProductDao.delNotEqIds(ids,originId);

        if (saveList != null && saveList.size() > 0){
            i = umsMemberOriginProductDao.batchSave(saveList);
        }
        if (updateList != null && updateList.size() > 0){
            i = umsMemberOriginProductDao.batchUpdate(updateList);
        }
        return i;
    }

    /**
     * 检验唯一
     * lym
     * @param originProduct
     */
    private void isExistOriginProduct(UmsMemberOriginProduct originProduct){
        UmsMemberOriginProductExample example = new UmsMemberOriginProductExample();
        UmsMemberOriginProductExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(originProduct.getId())){
            criteria.andIdNotEqualTo(originProduct.getId());
        }
        criteria.andFkOriginIdEqualTo(originProduct.getFkOriginId());
        criteria.andProductNameEqualTo(originProduct.getProductName());
        criteria.andStateEqualTo(0);
        long i = umsMemberOriginProductMapper.countByExample(example);
        if (i > 0){
            throw new GlobalException(500,"产出物已存在!");
        }
    }
}
