package com.shinsoft.sto.service.impl.ware;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinsoft.sto.mapper.ware.ProdMasterMapper;
import com.shinsoft.sto.mapper.ware.ChangeRecordMapper;
import com.shinsoft.sto.model.system.ResponseCodeMsg;
import com.shinsoft.sto.model.ware.ChangeRecord;
import com.shinsoft.sto.model.ware.ProdMaster;
import com.shinsoft.sto.service.ware.ProdMasterService;
import com.shinsoft.sto.model.system.ResultJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * 材料信息 服务实现类
 * </p>
 *
 * @author system
 * @since 2024-12-05
 */
@Service
public class ProdMasterServiceImpl extends ServiceImpl<ProdMasterMapper, ProdMaster> implements ProdMasterService {

    @Autowired
    private  ChangeRecordMapper changeRecordMapper;

    @Override
    public ResultJSON query(int page, int rows, Map<String, Object> map) {
        try {
            Page<ProdMaster> pageParam = new Page<>(page, rows);
            IPage<ProdMaster> result = baseMapper.selectProdMasterPage(pageParam, map);
            return ResultJSON.success(result);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultJSON edit(ProdMaster prodMaster) {
        try {
            if (prodMaster == null || prodMaster.getId() == null || prodMaster.getId().trim().isEmpty()) {
                return ResultJSON.error("材料ID不能为空");
            }

            ProdMaster dbData = baseMapper.selectById(prodMaster.getId());
            if (dbData == null) {
                return ResultJSON.error("未找到对应的材料信息");
            }

            // 记录旧数据到变更表
            ChangeRecord record = new ChangeRecord();
            BeanUtils.copyProperties(dbData, record);
            changeRecordMapper.insert(record);
            //修改材料信息
            ProdMaster prodMaster1 = new ProdMaster();
            prodMaster1.setId(prodMaster.getId());
            prodMaster1.setQuantity(prodMaster.getQuantity());

            prodMaster1.setWeight(prodMaster.getWeight());

            prodMaster1.setAttrib_01(prodMaster.getAttrib_01());

            prodMaster1.setAttrib_02(prodMaster.getAttrib_02());

            prodMaster1.setAttrib_04(prodMaster.getAttrib_04());

            prodMaster1.setAttrib_05(prodMaster.getAttrib_05());

            prodMaster1.setOwnerCompany(prodMaster.getOwnerCompany());

            prodMaster1.setCustomerCompany(prodMaster.getCustomerCompany());

            prodMaster1.setDeliveryAddress(prodMaster.getDeliveryAddress());

            prodMaster1.setRemark_1(prodMaster.getRemark_1());

            baseMapper.updateById(prodMaster1);
            return ResultJSON.success(dbData);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultJSON.error(ResponseCodeMsg.CODE_EX, ex.getMessage());
        }
    }
}

