package com.fuint.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.common.dto.AccountInfo;
import com.fuint.common.dto.MtPurchaseOrderItemDto;
import com.fuint.common.util.AuthUserUtil;
import com.fuint.common.vo.MtPurchaseOrderItemVo;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.PageResult;
import com.fuint.repository.mapper.MtMaterialMapper;
import com.fuint.repository.mapper.MtPurchaseOrderMapper;
import com.fuint.repository.mapper.MtStoreMapper;
import com.fuint.repository.model.MtMaterial;
import com.fuint.repository.model.MtPurchaseOrder;
import com.fuint.repository.model.MtPurchaseOrderItem;
import com.fuint.common.service.PurchaseOrderItemService;
import com.fuint.repository.mapper.MtPurchaseOrderItemMapper;
import com.fuint.repository.model.MtStore;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;


/**
 * 材料采购订单项表服务接口
 *
 * Created by huangwen
 * CopyRight https://www.fuint.cn
 */
@Service
@AllArgsConstructor
public class PurchaseOrderItemServiceImpl extends ServiceImpl<MtPurchaseOrderItemMapper, MtPurchaseOrderItem> implements PurchaseOrderItemService {

    private static final Logger logger = LoggerFactory.getLogger(PurchaseOrderItemServiceImpl.class);

    private MtPurchaseOrderItemMapper mtPurchaseOrderItemMapper;
    private MtPurchaseOrderMapper mtPurchaseOrderMapper;
    private MtMaterialMapper mtMaterialMapper;;
    private MtStoreMapper mtStoreMapper;

   /**
    * 根据条件搜索材料采购订单项表
    *
    * @param  mtPurchaseOrderItemDto 查询参数
    * @throws BusinessCheckException
    * @return
    * */
    @Override
    public PageResult<MtPurchaseOrderItemVo> queryPurchaseOrderItemListByParam(MtPurchaseOrderItemDto mtPurchaseOrderItemDto) {
        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "登录信息不能为空");
        Assert.isTrue(mtPurchaseOrderItemDto != null, "查询参数不能为空");
        Assert.isTrue(StringUtils.isNotBlank(mtPurchaseOrderItemDto.getPurchaseNo()), "采购订单号不能为空");

        IPage<MtPurchaseOrderItem> page = new Page<>(mtPurchaseOrderItemDto.getPageNo() == null ? 1 : mtPurchaseOrderItemDto.getPageNo(), mtPurchaseOrderItemDto.getPageSize() == null ? 10 : mtPurchaseOrderItemDto.getPageSize());

        IPage<MtPurchaseOrderItem> itemPage = mtPurchaseOrderItemMapper.selectPage(page,new LambdaQueryWrapper<MtPurchaseOrderItem>()
                .eq(MtPurchaseOrderItem::getPurchaseNo, mtPurchaseOrderItemDto.getPurchaseNo()));
        return PageResult.<MtPurchaseOrderItemVo>builder()
                .data(BeanUtil.copyToList(ObjectUtils.isNotEmpty(itemPage.getRecords()) ? itemPage.getRecords() : new ArrayList<>(), MtPurchaseOrderItemVo.class))
                .currentPage((int) itemPage.getCurrent())
                .total((int)itemPage.getTotal())
                .build();
    }

    @Override
    @Transactional
    public void deleteOrderItemById(Long id) {

        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "登录信息不能为空");
        Assert.isTrue(id != null, "请选择要删除的订单项");

        MtPurchaseOrderItem item = mtPurchaseOrderItemMapper.selectById(id);
        Assert.isTrue(ObjectUtils.isNotEmpty(item), "订单项不存在");

        MtPurchaseOrder mtPurchaseOrder = mtPurchaseOrderMapper.selectOne(new LambdaQueryWrapper<MtPurchaseOrder>().eq(MtPurchaseOrder::getOrderNo, item.getPurchaseNo()));
        Assert.isTrue(ObjectUtils.isNotEmpty(mtPurchaseOrder), "该采购单不存在");
        Assert.isTrue(accountInfo.getStoreId()!=0 && mtPurchaseOrder.getStoreId() != accountInfo.getStoreId(), "暂无该餐厅权限");

        MtStore mtStore = mtStoreMapper.selectById(accountInfo.getStoreId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtStore), "该餐厅不存在");

        mtPurchaseOrder.setTotalPrice(mtPurchaseOrder.getTotalPrice() - item.getTotalPrice());

        int delete = mtPurchaseOrderItemMapper.deleteById(id);
        int delete1 = mtPurchaseOrderMapper.updateById(mtPurchaseOrder);

        Assert.isFalse(delete!=1||delete1!=1, "删除失败");
    }

    @Override
    @Transactional
    public void updateOrderItem(MtPurchaseOrderItemDto mtPurchaseOrderItemDto) {

        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "登录信息不能为空");
        Assert.isTrue(ObjectUtils.isNotEmpty(mtPurchaseOrderItemDto), "请选择要修改的订单项");
        Assert.isFalse(mtPurchaseOrderItemDto.getId()== null, "请选择要删除的订单项");

        MtPurchaseOrderItem item = BeanUtil.copyProperties(mtPurchaseOrderItemDto, MtPurchaseOrderItem.class);

        MtPurchaseOrderItem item1 = mtPurchaseOrderItemMapper.selectById(mtPurchaseOrderItemDto.getId());
        Assert.isTrue(ObjectUtils.isNotEmpty(item1), "订单项不存在");

        MtPurchaseOrder mtPurchaseOrder = mtPurchaseOrderMapper.selectOne(new LambdaQueryWrapper<MtPurchaseOrder>().eq(MtPurchaseOrder::getOrderNo, item.getPurchaseNo()));
        Assert.isTrue(ObjectUtils.isNotEmpty(mtPurchaseOrder), "该采购单不存在");
        Assert.isTrue(accountInfo.getStoreId()!=0 && mtPurchaseOrder.getStoreId() != accountInfo.getStoreId(), "暂无该餐厅权限");

        MtStore mtStore = mtStoreMapper.selectById(accountInfo.getStoreId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtStore), "该餐厅不存在");

        MtMaterial mtMaterial = mtMaterialMapper.selectById(item.getMaterialId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtMaterial), "该材料不存在");

        item.setUpdateTime(new Date());
        item.setUpdateUserId(String.valueOf(accountInfo.getId()));

        mtPurchaseOrder.setTotalPrice(mtPurchaseOrder.getTotalPrice() - item1.getTotalPrice() + item.getTotalPrice());

        int update = mtPurchaseOrderItemMapper.updateById(item);

        int update1 = mtPurchaseOrderMapper.updateById(mtPurchaseOrder);
    Assert.isFalse(update!=1 ||update1!=1, "修改失败");

    }

    @Override
    @Transactional
    public MtPurchaseOrderItemVo addOrderItem(MtPurchaseOrderItemDto mtPurchaseOrderItemDto) {
        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "登录信息不能为空");
        Assert.isTrue(ObjectUtils.isNotEmpty(mtPurchaseOrderItemDto), "参数不能为空");
        Assert.isFalse(mtPurchaseOrderItemDto.getPurchaseNo()== null, "请选择要添加到的订单");

        MtPurchaseOrderItem item = BeanUtil.copyProperties(mtPurchaseOrderItemDto, MtPurchaseOrderItem.class);

        MtPurchaseOrder mtPurchaseOrder = mtPurchaseOrderMapper.selectOne(new LambdaQueryWrapper<MtPurchaseOrder>().eq(MtPurchaseOrder::getOrderNo, item.getPurchaseNo()));
        Assert.isTrue(ObjectUtils.isNotEmpty(mtPurchaseOrder), "该采购单不存在");
        Assert.isTrue(accountInfo.getStoreId()!=0 && mtPurchaseOrder.getStoreId() != accountInfo.getStoreId(), "暂无该餐厅权限");

        MtStore mtStore = mtStoreMapper.selectById(accountInfo.getStoreId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtStore), "该餐厅不存在");

        MtMaterial mtMaterial = mtMaterialMapper.selectById(item.getMaterialId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtMaterial), "该材料不存在");

        item.setCreateTime(new Date());
        item.setCreateUserId(String.valueOf(accountInfo.getId()));

        int insert = mtPurchaseOrderItemMapper.insert(item);
        Assert.isFalse(insert!=1, "添加失败");

        mtPurchaseOrder.setTotalPrice(mtPurchaseOrder.getTotalPrice()  + item.getTotalPrice());

        int update = mtPurchaseOrderMapper.updateById(mtPurchaseOrder);
        Assert.isFalse(update!=1, "添加失败");

        return BeanUtil.copyProperties(item, MtPurchaseOrderItemVo.class);
    }
}
