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


import com.steerinfo.baseinfo.dto.SelectMatterDemo;
import com.steerinfo.baseinfo.meterbasebillprint.mapper.MeterBaseBillPrintMapper;
import com.steerinfo.baseinfo.meterbasebillprint.model.MeterBaseBillPrint;
import com.steerinfo.baseinfo.meterbasecustomersupplier.model.MeterBaseCustomerSupplier;
import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.model.MeterBaseMatterInfo;
import com.steerinfo.baseinfo.meterbasematterinfo.service.IMeterBaseMatterInfoService;
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.framework.user.UserPayload;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

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

    //物料对应的打印张数
    @Autowired
    private MeterBaseBillPrintMapper meterBaseBillPrintMapper;

    @Resource
    private MeterBaseMatterInfoMapper meterBaseMatterInfoMapper;

    @Override
    protected IBaseMapper<MeterBaseMatterInfo, String> getMapper() {
        return meterBaseMatterInfoMapper;
    }

    @Override
    public String checkandadd(MeterBaseMatterInfo model) {
        if (StringUtils.isEmpty(model.getMatterName())) {
            return "物料名称不能为空";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode()) && model.getMeterCode().contains("000")) {
            return "物料的计量码不允许包含【000】，此编号已被临时委托使用！";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode())) {
            HashMap<String, Object> obj2 = new HashMap<>();
            obj2.put("meterCode", model.getMeterCode());
            List<MeterBaseMatterInfo> com2 = meterBaseMatterInfoMapper.selectByParameters(obj2);
            if (com2 != null && com2.size() > 0) {
                return "该计量码已存在！";
            }
        }
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("matterName", model.getMatterName());
        List<MeterBaseMatterInfo> com1 = meterBaseMatterInfoMapper.selectByParameters(obj1);
        if (!com1.isEmpty()) {
            return "该物料名称已存在";
        }
        String no = meterBaseMatterInfoMapper.GetNewID();
        model.setMatterNo(no);
        model.setCreateTime(new Date());
        model.setValidFlag("1");//有效
        meterBaseMatterInfoMapper.insert(model);

        //默认打印一张小票
        MeterBaseBillPrint meterBaseBillPrint = new MeterBaseBillPrint();
        meterBaseBillPrint.setMatterNo(model.getMatterNo()); //客户端是默认用编码查询打印张数的
        Map<String, Object> mapPrint = FieldsCollector.beanToMap(meterBaseBillPrint);
        List<MeterBaseBillPrint> printList = meterBaseBillPrintMapper.selectByParameters(mapPrint);
        if (printList != null && printList.size() > 0) {
            return "";
        }
        meterBaseBillPrint.setMatterName(model.getMatterName());
        String printNo = meterBaseBillPrintMapper.GetNewID();
        meterBaseBillPrint.setBillPrintNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + printNo);
        meterBaseBillPrint.setWeightTypeNo("001008003");
        meterBaseBillPrint.setWeightTypeName("净重");
        meterBaseBillPrint.setPrintNum((short) 2);
        meterBaseBillPrint.setCreateManNo("system");
        meterBaseBillPrint.setCreateManName("新增物料自动创建");
        meterBaseBillPrint.setCreateTime(new Date());
        meterBaseBillPrintMapper.insertSelective(meterBaseBillPrint);
        return "";
    }

    @Override
    public String checkandupdate(String id, MeterBaseMatterInfo model) {
        if (model.getMatterName() == null || model.getMatterName().equals("")) {
            return "物料名称不能为空";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode()) && model.getMeterCode().contains("000")) {
            return "物料的计量码不允许包含【000】，此编号已被临时委托使用！";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode())) {
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("meterCode", model.getMeterCode());
            obj.put("matterNo", id);
            List<MeterBaseMatterInfo> com = meterBaseMatterInfoMapper.selectForUpdate(obj);
            if (!com.isEmpty() && com.size() > 1) {
                return "该计量码已存在！";
            }
            if (!com.isEmpty() && com.size() == 1 && !com.get(0).getMatterNo().equals(id)) {
                return "该计量码已存在！";
            }
        }

        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("matterName", model.getMatterName());
        obj1.put("matterNo", id);
        List<MeterBaseMatterInfo> com = meterBaseMatterInfoMapper.selectForUpdate(obj1);
        if (!com.isEmpty() && com.size() > 1) {
            return "该物料名称已存在！";
        }
        if (!com.isEmpty() && com.size() == 1 && !com.get(0).getMatterNo().equals(id)) {
            return "该物料名称已存在！";
        }
        model.setUpdateTime(new Date());
        meterBaseMatterInfoMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String Recovery(List<MeterBaseMatterInfo> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseMatterInfo model : models) {
                model.setValidFlag("1");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseMatterInfoMapper.updateByPrimaryKey(model);

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

    @Override
    public String abolish(List<MeterBaseMatterInfo> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseMatterInfo model : models) {
                model.setValidFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseMatterInfoMapper.updateByPrimaryKey(model);

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

    @Override
    public PageList<MeterBaseMatterInfo> selectcorrelation(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.selectcorrelation();
        PageList pageInfo = new PageList(list);
        return (pageInfo);
    }

    @Override
    public PageList<MeterBaseMatterInfo> diffscorrelation(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.diffscorrelation();
        PageList pageInfo = new PageList(list);
        return (pageInfo);
    }


    @Override
    public PageList<MeterBaseMatterInfo> timecorRelation(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.timecorRelation();
        PageList pageInfo = new PageList(list);
        return (pageInfo);
    }

    @Override
    public List<MeterBaseMatterInfo> queryAll(Map<String, Object> paras) {
        List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.selectByParAndNum(paras);
        return list;
    }

    @Override
    @Transactional
    public String useTare(List<MeterBaseMatterInfo> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            for (MeterBaseMatterInfo model : models) {
                model.setAllowedTareWeight("1");
                model.setUpdateManName(userName);
                model.setUpdateManNo(userId);
                model.setUpdateTime(new Date());
                meterBaseMatterInfoMapper.updateByPrimaryKeySelective(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    @Transactional
    public String unTare(List<MeterBaseMatterInfo> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            for (MeterBaseMatterInfo model : models) {
                model.setAllowedTareWeight("0");
                model.setUpdateManName(userName);
                model.setUpdateManNo(userId);
                model.setUpdateTime(new Date());
                meterBaseMatterInfoMapper.updateByPrimaryKeySelective(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    @Transactional
    public String useAddWeight(List<MeterBaseMatterInfo> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            for (MeterBaseMatterInfo model : models) {
                model.setAllowedAddWeight("1");
                model.setUpdateManName(userName);
                model.setUpdateManNo(userId);
                model.setUpdateTime(new Date());
                meterBaseMatterInfoMapper.updateByPrimaryKeySelective(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    @Transactional
    public String unAddWeight(List<MeterBaseMatterInfo> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            for (MeterBaseMatterInfo model : models) {
                model.setAllowedAddWeight("0");
                model.setUpdateManName(userName);
                model.setUpdateManNo(userId);
                model.setUpdateTime(new Date());
                meterBaseMatterInfoMapper.updateByPrimaryKeySelective(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public PageList<MeterBaseMatterInfo> selectByMeterCode(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.selectByMeterCode();
        PageList pageInfo = new PageList(list);
        return (pageInfo);
    }

    @Override
    public PageList<MeterBaseMatterInfo> listLike(SelectMatterDemo model) {
        /*HashMap<String, Object> obj = new HashMap<>();
        obj.put("matterName", model.getMatterName());
        obj.put("meterCode", model.getMeterCode());*/
        try {
            Map<String, Object> map = FieldsCollector.beanToMap(model);
            PageHelper.startPage(model.getPageNum(), model.getPageSize());
            List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.selectLikeByParameters(map);
            PageList pageInfo = new PageList(list);
            return (pageInfo);
        } catch (Exception ex) {
            throw new MarkerMetException(500, "impl异常测试！！");
        }

    }

    @Override
    public PageList<MeterBaseMatterInfo> listMeterCode(SelectMatterDemo model) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("matterName", model.getMatterName());
        PageHelper.startPage(model.getPageNum(), model.getPageSize());
        List<MeterBaseMatterInfo> list = meterBaseMatterInfoMapper.listMeterCode(obj);
        PageList pageInfo = new PageList(list);
        return (pageInfo);
    }

    @Override
    public int doUpdateBeltType(List<MeterBaseMatterInfo> list) {
        if (list == null) {
            throw new MarkerMetException(500, "参数不允许为空!!");
        }
        return meterBaseMatterInfoMapper.doUpdateBeltType(list);
    }

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