package com.steerinfo.meterwork.meterworkprewgttype.service.impl;

import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.meterwork.meterworkprewgttype.model.MeterWorkPreWgttype;
import com.steerinfo.meterwork.meterworkprewgttype.mapper.MeterWorkPreWgttypeMapper;
import com.steerinfo.meterwork.meterworkprewgttype.service.IMeterWorkPreWgttypeService;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import com.sun.net.httpserver.Authenticator;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

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

    @Resource
    private MeterWorkPreWgttypeMapper meterWorkPreWgttypeMapper;
    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    @Override
    protected IBaseMapper<MeterWorkPreWgttype, String> getMapper() {
        return meterWorkPreWgttypeMapper;
    }

    @Transactional
    @Override
    public RESTfulResult addOrAbolish(MeterWorkPreWgttype model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        rm.setMessage("操作异常！！");
        // 数据验证
        if (StringUtils.isEmpty(model.getCarNo())) {
            rm.setMessage("车号不能为空，请确认！！");
        }
        else if (StringUtils.isEmpty(model.getWeightTypeNo()) || StringUtils.isEmpty(model.getWeightTypeName())) {
            rm.setMessage("重量类型编号/名称不能为空，请确认！！");
        }
        else if (StringUtils.isEmpty(model.getCreateManName())) {
            rm.setMessage("创建人不能为空，请确认！！");
        }
        else if (StringUtils.isEmpty(model.getCreateManPhone())) {
            rm.setMessage("创建人手机号不能为空，请确认！！");
        }
        try {
            // 根据车号，日期，状态未使用，判断当天当车是否有预处理数据
            HashMap query = new HashMap();
            query.put("carNo", model.getCarNo());
            // 日期转字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            Date date = calendar.getTime();
            String dateStringParse = sdf.format(date);
            query.put("createTime", dateStringParse);
            query.put("valueFlag", "1");
            List<MeterWorkPreWgttype> list = meterWorkPreWgttypeMapper.selectByParameters(query);

            // 如果没有，新增记录,如果有，作废原纪录，再新建记录
            if(list.size() >= 1) {
                // 作废原纪录
                MeterWorkPreWgttype tempModel = list.get(0);
                tempModel.setValueFlag("0");
                tempModel.setUpdateManName(model.getCreateManName());
                tempModel.setUpdateManPhone(model.getCreateManPhone());
                tempModel.setUpdateTime(new Date());
                meterWorkPreWgttypeMapper.updateByPrimaryKey(tempModel);
            }
            //修改主键为yyyyMMddHHmmssSSS+随机数
//            String no = meterWorkPreWgttypeMapper.GetNewID();
            String no = PublicMethod.IdCreate(3);
//            model.setWgtTypeId(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
            model.setWgtTypeId(no);
            model.setCreateTime(new Date());
            model.setValueFlag("1");
            model.setSubmitFlag("0");

            meterWorkPreWgttypeMapper.insert(model);
            rm.setSucceed();
            rm.setMessage("操作成功！！");
            rm.setData(model);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return rm;
    }

    @Override
    public RESTfulResult getCarPreInfo(String carNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        //存预约信息
        HashMap<String, Object> preWgtType = new HashMap<>();
        //查找车号的毛皮重预约信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("carNo", carNo);
        map.put("valueFlag", "1");
        List<MeterWorkPreWgttype> modelList01 = meterWorkPreWgttypeMapper.selectByParameters(map);
        if(modelList01.size() >= 1) {
            if (modelList01.get(0).getWeightTypeName().equals("毛重")) {
                    HashMap<String, Object> model = new HashMap<>();
                    model.put("carNo", carNo);
                    model.put("valueFlag", "1");
                    model.put("lineDesc", "1");
                    List<PreTrackScale> scale = preTrackScaleMapper.selectByMap(model);
                    if (scale.size() >= 1) {
                        preWgtType.put("grossWgt", scale);
                        preWgtType.put("wgtType", modelList01);
                        rm.setSucceed();
                        rm.setData(preWgtType);
                        rm.setCode("200");
                        rm.setMessage("操作成功");
                        return rm;
                    } else if (scale.size() == 0) {
                        rm.setMessage("根据车号未查询到相应的委托信息，请确认！！");
                        return rm;
                    }
            } else if (modelList01.get(0).getWeightTypeName().equals("皮重")) {
                preWgtType.put("wgtType", modelList01);
                rm.setSucceed();
                rm.setData(preWgtType);
                rm.setCode("200");
                rm.setMessage("操作成功");
                return rm;
            }
        }else if(modelList01.size() == 0) {
            map.remove("valueFlag");
            map.put("valueFlag", "2");
            rm.setMessage("未查询到相应车号的预约信息，请确认！！");
            return rm;
        }
        return rm;
    }

    @Override
    public RESTfulResult abolishPreWgtType(String carNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        HashMap<String, Object> map = new HashMap<>();
        map.put("carNo", carNo);
        map.put("valueFlag", "1");
        List<MeterWorkPreWgttype> models = meterWorkPreWgttypeMapper.selectByParameters(map);
        if(models.size() >= 1){
            for(MeterWorkPreWgttype model: models) {
                model.setValueFlag("0");
                if(model.getWeightTypeName().equals("毛重")){
                    HashMap<String, Object> preTrackMap = new HashMap<>();
                    preTrackMap.put("carNo", carNo);
                    preTrackMap.put("lineDesc", "1");
                    List<PreTrackScale> scales = preTrackScaleMapper.selectByParameters(preTrackMap);
                    if (scales.size() >= 1) {
                        for(PreTrackScale scale: scales){
                            scale.setLineDesc("0");
                            preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                        }
                    }
                }
                meterWorkPreWgttypeMapper.updateByPrimaryKey(model);
            }
        }
        rm.setSucceed();
        rm.setData(null);
        rm.setCode("200");
        rm.setMessage("操作成功!!");
        return rm;
    }
}

