package com.yh.saas.plugin.yiliangyiyun.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.plugin.yiliangyiyun.constant.NumberConstant;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.PriceConfirmationSheet;
import com.yh.saas.plugin.yiliangyiyun.mapper.PriceConfirmationSheetMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IPriceConfirmationSheetService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 点价确认单 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-08-02
 */
@Service
public class PriceConfirmationSheetServiceImpl extends ServiceImpl<PriceConfirmationSheetMapper, PriceConfirmationSheet> implements IPriceConfirmationSheetService {

    /**
     * 查看确认单
     */
    @Override
    public List<PriceConfirmationSheet> selectConfirmationSheet(String contractNo) {
        List<PriceConfirmationSheet> priceConfirmationSheetList = this.selectList(new EntityWrapper<PriceConfirmationSheet>()
                .eq(PriceConfirmationSheet.QueryFiles.CONTRACT_NO, contractNo)
                .eq(PriceConfirmationSheet.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        return priceConfirmationSheetList;
    }

    /**
     * 添加点价确认单
     *
     * @param priceConfirmationSheet
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPriceConfirmationSheet(PriceConfirmationSheet priceConfirmationSheet) {
        //新增主键id
        priceConfirmationSheet.setId(IdGenerator.generateUUID());
        //上传了附件
        if (!StringUtils.isEmpty(priceConfirmationSheet.getAddressUrl())) {
            priceConfirmationSheet.setEnclosureTypeFlag(StatusEnum.UPLOADED.getFlag());
            priceConfirmationSheet.setEnclosureType(StatusEnum.UPLOADED.getName());
        }
        //未上传附件
        else {
            priceConfirmationSheet.setEnclosureTypeFlag(StatusEnum.Not_UPLOADED.getFlag());
            priceConfirmationSheet.setEnclosureType(StatusEnum.Not_UPLOADED.getName());
        }
        priceConfirmationSheet.setContractNo(priceConfirmationSheet.getContractNo());
        // 操作主表数据
        boolean one = this.insert(priceConfirmationSheet);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

    /**
     * 编辑点价确认单
     *
     * @param priceConfirmationSheet
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editPriceConfirmationSheet(PriceConfirmationSheet priceConfirmationSheet) {
        //上传了附件
        if (!StringUtils.isEmpty(priceConfirmationSheet.getAddressUrl())) {
            priceConfirmationSheet.setEnclosureTypeFlag(StatusEnum.UPLOADED.getFlag());
            priceConfirmationSheet.setEnclosureType(StatusEnum.UPLOADED.getName());
        }
        boolean one = this.updateById(priceConfirmationSheet);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

    /**
     * 删除点价确认单
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePriceConfirmationSheet(String id) {
        //查询点价确认单
        PriceConfirmationSheet priceConfirmationSheet = this.selectById(id);
        if (priceConfirmationSheet != null) {
            //删除点价确认单
            this.deleteById(priceConfirmationSheet.getId());
        }
    }

    /**
     * 查看确认单信息
     *
     * @param id
     * @return
     */
    @Override
    public PriceConfirmationSheet getInfo(String id) {
        // 查看信息
        PriceConfirmationSheet priceConfirmationSheet = this.selectById(id);
        return priceConfirmationSheet;
    }
}
