package com.steerinfo.baseinfo.meterbasecustomersupplier.service.impl;

import com.steerinfo.baseinfo.constants.DbConstants;
import com.steerinfo.baseinfo.dto.MeterBaseCustomerSupplierDto;
import com.steerinfo.baseinfo.meterbasecustomersupplier.mapper.MeterBaseCustomerSupplierMapper;
import com.steerinfo.baseinfo.meterbasecustomersupplier.model.MeterBaseCustomerSupplier;
import com.steerinfo.baseinfo.meterbasecustomersupplier.service.IMeterBaseCustomerSupplierService;
import com.steerinfo.baseinfo.meterbaseloadpoint.mapper.MeterBaseLoadPointMapper;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MeterBaseCustomerSupplier服务实现：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-06-15 11:14
 * 类描述
 * 修订历史：
 * 日期：2020-06-15
 * 作者：generator
 * 参考：
 * 描述：MeterBaseCustomerSupplier服务实现
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@Service(value = "meterBaseCustomerSupplierService")
public class MeterBaseCustomerSupplierServiceImpl extends BaseServiceImpl<MeterBaseCustomerSupplier, String> implements IMeterBaseCustomerSupplierService {

    @Resource
    private MeterBaseCustomerSupplierMapper meterBaseCustomerSupplierMapper;

    @Resource
    private MeterBaseLoadPointMapper meterBaseLoadPointMapper;

    @Override
    protected IBaseMapper<MeterBaseCustomerSupplier, String> getMapper() {
        return meterBaseCustomerSupplierMapper;
    }

    @Override
    public String checkandadd(MeterBaseCustomerSupplier model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("customerSupplierLogin", model.getCustomerSupplierLogin());
        List<MeterBaseCustomerSupplier> com1 = meterBaseCustomerSupplierMapper.selectByParameters(obj1);
        if (!com1.isEmpty()) {
            return "该账号已存在！！";
        }
        HashMap<String, Object> obj2 = new HashMap<>();
        obj2.put("customerSupplierName", model.getCustomerSupplierName());
        List<MeterBaseCustomerSupplier> com2 = meterBaseCustomerSupplierMapper.selectByParameters(obj2);
        if (!com2.isEmpty()) {
            return "此客商已存在";
        }
        String no = meterBaseCustomerSupplierMapper.GetNewID();
        model.setCustomerSupplierNo(no);
        meterBaseCustomerSupplierMapper.insertSelective(model);
        return "";
    }

    @Override
    public String Recovery(List<MeterBaseCustomerSupplier> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCustomerSupplier model : models) {
                model.setValidFlag("1");//有效
                model.setCheckFlag("1");//已审核
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseCustomerSupplierMapper.updateByPrimaryKey(model);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String audit(List<MeterBaseCustomerSupplier> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCustomerSupplier model : models) {
                model.setCheckFlag("1");//已审核
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseCustomerSupplierMapper.updateByPrimaryKey(model);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String unaudit(List<MeterBaseCustomerSupplier> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCustomerSupplier model : models) {
                model.setCheckFlag("0");//未审核
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseCustomerSupplierMapper.updateByPrimaryKey(model);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String abolish(List<MeterBaseCustomerSupplier> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCustomerSupplier model : models) {
                model.setValidFlag("0");//无效
                model.setCheckFlag("0");//未审核
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseCustomerSupplierMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id, MeterBaseCustomerSupplier model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("customerSupplierLogin", model.getCustomerSupplierLogin());
        obj1.put("customerSupplierNo", id);
        List<MeterBaseCustomerSupplier> com1 = meterBaseCustomerSupplierMapper.selectForUpdate(obj1);
        if (!com1.isEmpty() && !com1.get(0).getCustomerSupplierNo().equals(model.getCustomerSupplierNo())) {
            return "该账号已存在！！";
        }
        HashMap<String, Object> obj2 = new HashMap<>();
        obj2.put("customerSupplierName", model.getCustomerSupplierName());
//        obj2.put("customerSupplierNo", id);
        List<MeterBaseCustomerSupplier> com2 = meterBaseCustomerSupplierMapper.selectForUpdate(obj2);
        if (!com2.isEmpty() && !com2.get(0).getCustomerSupplierNo().equals(model.getCustomerSupplierNo())) {
            return "此客商已存在";
        }
        meterBaseCustomerSupplierMapper.updateByPrimaryKey(model);
        return "";
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterBaseCustomerSupplierDto> selectIn() {
        try {
            List<MeterBaseCustomerSupplierDto> mbcsds = meterBaseCustomerSupplierMapper.selectComp1(DbConstants.CUS);
            List<MeterBaseCustomerSupplierDto> mbcsds1 = meterBaseCustomerSupplierMapper.selectComp2();
            for (MeterBaseCustomerSupplierDto mbcsd : mbcsds1) {
                mbcsds.add(mbcsd);
            }
            return mbcsds;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterBaseCustomerSupplierDto> selectOut() {
        try {
            List<MeterBaseCustomerSupplierDto> mbcsds = meterBaseCustomerSupplierMapper.selectComp1(DbConstants.SUP);
            List<MeterBaseCustomerSupplierDto> mbcsds1 = meterBaseCustomerSupplierMapper.selectComp2();
            for (MeterBaseCustomerSupplierDto mbcsd : mbcsds1) {
                mbcsds.add(mbcsd);
            }
            return mbcsds;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    public PageList<MeterBaseCustomerSupplier> selectByType(HashMap parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseCustomerSupplier> rows = meterBaseCustomerSupplierMapper.selectByType();
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<MeterBaseCustomerSupplier> queryAll(Map<String, Object> parmas) {
        parmas.put("checkFlag", "1");
        return meterBaseCustomerSupplierMapper.selectByParameters(parmas);
    }

    private String check(MeterBaseCustomerSupplier model) {
        if (model == null) {
            return "数据为空";
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierName())) {
            return "客商名称不能为空！";
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierType())) {
            return "客商类型不能为空！";
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierLogin())) {
            return "账号不能为空！";
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierPassword())) {
            return "密码不能为空！";
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierNo())) {
            model.setCreateTime(new Date());
            model.setValidFlag("1");//有效
            model.setCheckFlag("0");//未审核
        } else {
            model.setUpdateTime(new Date());
        }
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterBaseCustomerSupplier> selectPrint() {
        try {
            List<MeterBaseCustomerSupplier> mbcsds = meterBaseCustomerSupplierMapper.selectPrint();
            return mbcsds;
        } catch (MarkerMetException e) {
            throw e;
        }
    }
}
