package com.hzs.sale.modifyrecord.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EModifyRecordStatus;
import com.hzs.common.core.enums.EModifyRecordType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.sale.modifyRecord.BdModifyRecord;
import com.hzs.common.domain.sale.wares.BdWares;
import com.hzs.common.domain.system.sys.SysUser;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.modifyrecord.mapper.BdModifyRecordMapper;
import com.hzs.sale.modifyrecord.service.IBdModifyRecordService;
import com.hzs.sale.product.controller.manage.params.ProductParams;
import com.hzs.sale.product.service.IBdProductService;
import com.hzs.sale.wares.param.WaresParams;
import com.hzs.sale.wares.service.IBdWaresService;
import com.hzs.system.sys.IUserServiceApi;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 修改记录 服务实现类
 *
 * @author hzs
 * @since 2023-02-27
 */
@Service
public class BdModifyRecordServiceImpl extends ServiceImpl<BdModifyRecordMapper, BdModifyRecord> implements IBdModifyRecordService {

    @Autowired
    private IBdWaresService waresService;
    @Autowired
    private IBdProductService productService;
    @Autowired
    private IBdModifyRecordService modifyRecordService;

    @DubboReference
    IUserServiceApi userServiceApi;

    /**
     * 商品审核
     *
     * @param mr
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void examineAndVerify(BdModifyRecord mr, LoginUser loginUser) {
        BdModifyRecord modifyRecord = modifyRecordService.getById(mr.getPkId());
        if (mr.getAuditStatus().equals(EModifyRecordStatus.APPROVE.getValue())) {
            // 审核通过执行
            if (modifyRecord.getType().equals(EModifyRecordType.WARES_ADD.getValue())) {
                WaresParams waresParams = JSONUtil.toBean(modifyRecord.getNewValue(), WaresParams.class);
                BdWares wares = waresService.saveWares(waresParams, loginUser);
                modifyRecord.setPkWares(wares.getPkId());
                modifyRecordService.updateById(modifyRecord);
            } else if (modifyRecord.getType().equals(EModifyRecordType.WARES_UPDATE.getValue())) {
                WaresParams waresParams = JSONUtil.toBean(modifyRecord.getNewValue(), WaresParams.class);
                waresService.updateWares(waresParams, loginUser);
            } else if (modifyRecord.getType().equals(EModifyRecordType.PRODUCT_ADD.getValue())) {
                // 产品新增
                ProductParams productParams = JSONUtil.toBean(modifyRecord.getNewValue(), ProductParams.class);
                productService.saveProduct(productParams, loginUser);
            } else if (modifyRecord.getType().equals(EModifyRecordType.PRODUCT_UPDATE.getValue())) {
                // 产品修改
                ProductParams productParams = JSONUtil.toBean(modifyRecord.getNewValue(), ProductParams.class);
                productService.updateProduct(productParams, loginUser);
            }
        }
        R<SysUser> user = userServiceApi.selectUserById(SecurityUtils.getUserId());
        modifyRecord.setModifiedTime(DateUtils.currentDateTime());
        modifyRecord.setPkModified(loginUser.getUserId());
        modifyRecord.setPkReview(loginUser.getUserId());
        modifyRecord.setAuditStatus(mr.getAuditStatus());
        modifyRecord.setReviewRemarks(mr.getReviewRemarks());
        modifyRecord.setReviewTime(DateUtils.currentDateTime());
        modifyRecord.setReviewName(loginUser.getUsername());
        modifyRecord.setPkReviewDept(user.getData().getPkDept());
        modifyRecord.setReviewDeptName(user.getData().getDeptName());
        modifyRecordService.updateById(modifyRecord);
    }

}
