package com.prolog.rdc.tms.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.dao.mapper.SySParameterMapper;
import com.prolog.rdc.tms.model.dto.SySParameterDTO;
import com.prolog.rdc.tms.model.entity.SySParameterModel;
import com.prolog.rdc.tms.model.vo.SySParameterVO;
import com.prolog.rdc.tms.service.SySParameterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SySParameterServiceImpl implements SySParameterService {

    @Autowired
    private SySParameterMapper mapper;

    @Override
    public SySParameterDTO queryParameter() {
        SySParameterModel parameterModel = mapper.findList();
        SySParameterDTO dto = new SySParameterDTO();
        if (ObjectUtil.isNotEmpty(parameterModel)) {
            BeanUtils.copyProperties(parameterModel,dto);
            dto.setIsTaxName(parameterModel.getIsTax() == 1?"含税单价":"未税单价");
            return dto;
        }
        return dto;
    }

    @Override
    public void saveOrUpdate(SySParameterVO vo) {
        UserEntity user = PlgFxUsrUtils.getUserEntity();
        // 修改
        SySParameterModel model = mapper.findList();
        if (ObjectUtil.isNotEmpty(model)){
            BeanUtils.copyProperties(vo,model);
            model.setUpdateperson(user.getNickname());
            model.setUpdatetime(DateUtil.date());
            mapper.update(model);
        } else {
            // 新增
            SySParameterModel parameterModel = new SySParameterModel();
            BeanUtils.copyProperties(vo,parameterModel);
            parameterModel.setCreateperson(user.getNickname());
            parameterModel.setCreatetime(DateUtil.date());
            parameterModel.setUpdatetime(DateUtil.date());
            parameterModel.setUpdateperson(user.getNickname());
            parameterModel.setEnterpriseId(user.getEnterpriseId());
            parameterModel.setEnterpriseName(user.getEnterpriseName());
            mapper.save(parameterModel);
        }
    }
}