package com.steerinfo.appservice.appmeterbasecustomersupplier.service.impl;

import com.steerinfo.appservice.appmeterbasecustomersupplier.mapper.AppMeterBaseCustomerSupplierMapper;
import com.steerinfo.appservice.appmeterbasecustomersupplier.model.AppMeterBaseCustomerSupplier;
import com.steerinfo.appservice.appmeterbasecustomersupplier.service.IAppMeterBaseCustomerSupplierService;
import com.steerinfo.baseinfo.meterbasecarrierunit.mapper.MeterBaseCarrierUnitMapper;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.util.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 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 = "appServiceMeterBaseCustomerSupplierService")
@Primary
public class AppMeterBaseCustomerSupplierServiceImpl extends BaseServiceImpl<AppMeterBaseCustomerSupplier, String> implements IAppMeterBaseCustomerSupplierService {

    @Resource
    private AppMeterBaseCustomerSupplierMapper appMeterBaseCustomerSupplierMapper;

    @Resource
    private MeterBaseCarrierUnitMapper meterBaseCarrierUnitMapper;

    @Override
    protected IBaseMapper<AppMeterBaseCustomerSupplier, String> getMapper() {
        return appMeterBaseCustomerSupplierMapper;
    }

    @Override
    @Transactional
    public RESTfulResult login(AppMeterBaseCustomerSupplier model) {
        RESTfulResult rm = new RESTfulResult();
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("customerSupplierLogin", model.getCustomerSupplierLogin());
        obj.put("customerSupplierPassword", model.getCustomerSupplierPassword());
        obj.put("validFlag", "1");//有效
        List<AppMeterBaseCustomerSupplier> mbcss = appMeterBaseCustomerSupplierMapper.selectByParameters(obj);
        if (mbcss != null && mbcss.size() > 0) {
            if (StringUtils.isNotEmpty(mbcss.get(0).getCheckFlag()) && "0".equals(mbcss.get(0).getCheckFlag())) {
                rm.setMessage("账号未通过审核，不允许进行登录！");
                rm.setCode("500");
                return rm;
            }
        }
        if (mbcss == null || mbcss.isEmpty()) {
            rm.setMessage("账号不存在或密码错误！！");
            rm.setCode("500");
        } else {
            rm.setCode("0");
            rm.setData(mbcss.get(0));
        }
        return rm;
    }

    @Override
    @Transactional
    public RESTfulResult modifyPassword(AppMeterBaseCustomerSupplier model) {
        RESTfulResult rm = new RESTfulResult();
        AppMeterBaseCustomerSupplier mbcs = appMeterBaseCustomerSupplierMapper.selectByPrimaryKey(model.getCustomerSupplierNo());
        if (!mbcs.getCustomerSupplierPassword().equals(model.getCustomerSupplierPassword())) {
            rm.setData(null);
            rm.setCode("500");
            rm.setMessage("原密码错误！");
        } else {
            model.setCustomerSupplierPassword(model.getNewCustomerSupplierPassword());
            appMeterBaseCustomerSupplierMapper.updateByPrimaryKeySelective(model);
            rm.setData(model);
        }
        return rm;
    }

}
