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

import cn.hutool.core.lang.Assert;
import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.baseinfo.meterbaseuserspotconfig.mapper.MeterBaseUserSpotConfigMapper;
import com.steerinfo.baseinfo.meterbaseuserspotconfig.model.MeterBaseUserSpotConfig;
import com.steerinfo.baseinfo.meterbaseuserspotconfig.service.IMeterBaseUserSpotConfigService;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.meterworkcheckfirstactual.model.MeterWorkCheckFirstActual;
import com.steerinfo.util.PublicMethod;
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 java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Autowired(required = false)
    private MeterBaseUserSpotConfigMapper meterBaseUserSpotConfigMapper;

    @Autowired(required = false)
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    @Override
    protected IBaseMapper<MeterBaseUserSpotConfig, String> getMapper() {
        return meterBaseUserSpotConfigMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult addModel(List<MeterBaseUserSpotConfig> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            Assert.isTrue(models.size() >= 1, "用户站点分配信息不能为空，请重试");
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch(Exception e) {
                userId = "admin";
                userName = "admins";
            }
            int count = 0;
            for(MeterBaseUserSpotConfig model: models) {
                //根据秤点查询用户站点信息配置表,始终要保证秤点唯一对应一个人员信息
                HashMap<String, Object> map = new HashMap<>();
                map.put("baseSpotNo", model.getBaseSpotNo());
                map.put("baseSpotName", model.getBaseSpotName());
                List<MeterBaseUserSpotConfig> configModels = meterBaseUserSpotConfigMapper.selectByParameters(map);
                if(configModels.size() >= 1) {
                    MeterBaseUserSpotConfig config = configModels.get(0);
                    config.setUserId(model.getUserId());
                    config.setUserCode(model.getUserCode());
                    config.setUserName(model.getUserName());
                    config.setCreateManNo(userId);
                    config.setCreateManName(userName);
                    config.setCreateTime(new Date());
                    meterBaseUserSpotConfigMapper.updateByPrimaryKeySelective(config);
                }else {
                    model.setId(PublicMethod.IdCreate(3));
                    model.setCreateManNo(userId);
                    model.setCreateManName(userName);
                    model.setCreateTime(new Date());
                    meterBaseUserSpotConfigMapper.insertSelective(model);
                }
                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(userId);
                logModel.setCreateManName(userName);
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("web端-日工作量用户秤点信息配置表");
                logModel.setOperationFunction("秤点分配");
                logModel.setOperationId("");
                logModel.setOperationPredictionNo("");
                logModel.setOperationCarnNo("");
                logModel.setOperationAfterFix("用户账号：" + model.getUserCode() + ", 用户名称：" + model.getUserName() + ", 秤点编号：" + model.getBaseSpotNo() + ", 秤点名称："
                + model.getBaseSpotName());
                meterBaseOperationLogMapper.insertSelective(logModel);
                count ++;
            }
            rm.setSucceed(true);
            rm.setCode("200");
            rm.setData(count);
            rm.setMessage("操作成功，总共" + count + "条人员秤点信息分配完成");
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：" + e.getMessage());
        }
        return rm;
    }

    @Override
    public List<MeterBaseUserSpotConfig> noPage(HashMap<String, Object> map) {
        return meterBaseUserSpotConfigMapper.noPage(map);
    }

    @Override
    @Transactional
    public void deleteUserSpot(List<String> ids) {
        try {
            for (String id : ids) {

                String userId = "";
                String userName = "";
                try{
                    UserPayload userPayload = UserPayload.getCurrUser();
                    userId = userPayload.getUserCode();
                    userName = userPayload.getUserName();
                }catch(Exception e) {
                    userId = "admin";
                    userName = "admins";
                }
                MeterBaseUserSpotConfig model = meterBaseUserSpotConfigMapper.selectByPrimaryKey(id);
                meterBaseUserSpotConfigMapper.deleteByPrimaryKey(id);

                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(userId);
                logModel.setCreateManName(userName);
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("web端-日工作量用户秤点信息配置表");
                logModel.setOperationFunction("取消分配");
                logModel.setOperationId("");
                logModel.setOperationPredictionNo("");
                logModel.setOperationCarnNo("");
                logModel.setOperationAfterFix("用户账号：" + model.getUserCode() + ", 用户名称：" + model.getUserName() + ", 秤点编号：" + model.getBaseSpotNo() + ", 秤点名称："
                        + model.getBaseSpotName());
                meterBaseOperationLogMapper.insertSelective(logModel);
            }
        }catch (Exception e){
            throw e;
        }
    }
}
