package com.huatek.busi.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dto.RParamAttrBasicDataDto;
import com.huatek.busi.entity.RParamAttrBasicData;
import com.huatek.busi.mapper.RParamAttrBasicDataMapper;
import com.huatek.busi.service.IRParamAttrBasicDataService;
import com.huatek.busi.vo.RParamAttrBasicDataVo;
import com.huatek.busi.where.RParamAttrBasicDataSelectVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.frame.base.util.Constants;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@Service(interfaceClass = IRParamAttrBasicDataService.class)
@Transactional(rollbackFor = Exception.class)
public class RParamAttrBasicDataServiceImpl extends BaseDao<RParamAttrBasicData, String> implements IRParamAttrBasicDataService {

    @Resource
    private RParamAttrBasicDataMapper rParamAttrBasicDataMapper;

    @Override
    public Map<String, Object> findChildList(RParamAttrBasicDataSelectVo rParamAttrBasicDataSelectVo){
        rParamAttrBasicDataSelectVo.setDeleted(Constants.DEFAULT_NO);
        PageInfo<RParamAttrBasicDataDto> page = PageHelper.startPage(rParamAttrBasicDataSelectVo.getPage(), rParamAttrBasicDataSelectVo.getLimit())
                .doSelectPageInfo(() -> rParamAttrBasicDataMapper.findChildList(rParamAttrBasicDataSelectVo));
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, page.getTotal(), "BUSINESS.QUERY.SUCCESS", page.getList());
    }

    @Override
    public Map<String, Object> saveOrUpdateChild(String token, RParamAttrBasicDataVo rParamAttrBasicDataVo){
        RParamAttrBasicData rParamAttrBasicData = new RParamAttrBasicData();

        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        // 获取当前用户信息
        LocalUser user = UserUtil.getLocalUser(token);

        String msg;
        int count = 0, code;
        String cId = rParamAttrBasicDataVo.getId();
        if(StrUtil.isNotBlank(cId)){
            rParamAttrBasicData = this.get(cId);
            if(rParamAttrBasicData != null){
                // 评估字段规则
                String assessFieldRule = rParamAttrBasicData.getAssessFieldRule();
                rParamAttrBasicDataVo.setAssessFieldRule(assessFieldRule);
                rParamAttrBasicDataVo.setId(cId);
                BeanUtils.copyProperties(rParamAttrBasicDataVo, rParamAttrBasicData);
                if(user != null){
                    rParamAttrBasicData.setUpdateUser(user.getUid());
                    rParamAttrBasicData.setCreateTime(LocalDateTime.now());
                }
                count = this.updateLocal(rParamAttrBasicData);
                if(count > 0){
                    msg = "BUSINESS.UPDATE.SUCCESS";
                    code = ConstantUtil.REQUEST_SUCCESS;
                }else {
                    msg = "BUSINESS.UPDATE.FAIL";
                    code = ConstantUtil.REQUEST_FAIL;
                }
            }else {
                msg = "BUSINESS.UPDATE.FAIL";
                code = ConstantUtil.REQUEST_FAIL;
            }
        }else {
            BeanUtils.copyProperties(rParamAttrBasicDataVo, rParamAttrBasicData);
            // 查询评估字段规则最大值
            Long maxValue = rParamAttrBasicDataMapper.findMaxAssessFieldRule();
            rParamAttrBasicData.setAssessFieldRule(DecimalUtil.autoNumber(maxValue));
            rParamAttrBasicData.setId(Uuid.getUUID());
            if(user != null){
                rParamAttrBasicData.setCreateUser(user.getUid());
                rParamAttrBasicData.setCreateTime(LocalDateTime.now());
            }
            rParamAttrBasicData.setDeleted(Constants.DEFAULT_NO);
            count = this.add(rParamAttrBasicData);
            if(count > 0){
                msg = "BUSINESS.INSERT.SUCCESS";
                code = ConstantUtil.REQUEST_SUCCESS;
            }else {
                msg = "BUSINESS.INSERT.FAIL";
                code = ConstantUtil.REQUEST_FAIL;
            }
        }
        return ResultUtil.put(code, msg, count);
    }

    @Override
    public Map<String, Object> findOneChild(String token, String id){

        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        if(StrUtil.isEmpty(id)){
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.ID.NOTNULL", "");
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", this.get(id));
    }

    @Override
    public Map<String, Object> deleteChild(String token, List<String> ids){
        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        try {
            for (String id :ids){
                RParamAttrBasicData rParamAttrBasicData = this.get(id);
                if(rParamAttrBasicData != null){
                    rParamAttrBasicData.setDeleted(Constants.DEFAULT_YES);
                    this.updateLocal(rParamAttrBasicData);
                }
            }
        }catch (Exception e){
            log.error("删除异常", e);
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.DELETE.FAIL", "");
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.DELETE.SUCCESS", "");
    }

    @Override
    public RParamAttrBasicData findRParamAttrBasicData(String mainEquId, String testItem, String offlineParam, String unit){
        return rParamAttrBasicDataMapper.findRParamAttrBasicData(mainEquId, testItem, offlineParam, unit);
    }

    @Override
    public int insert(RParamAttrBasicData rParamAttrBasicData){
        return this.add(rParamAttrBasicData);
    }
}
