package com.sqx.modules.material.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.sqx.common.utils.Result;
import com.sqx.modules.artificer.dao.OrdersDao;
import com.sqx.modules.artificer.entity.Orders;
import com.sqx.modules.material.dao.MaterialArtificerMapper;
import com.sqx.modules.material.dao.MaterialMapper;
import com.sqx.modules.material.dao.MaterialMassageMapper;
import com.sqx.modules.material.entity.*;
import com.sqx.modules.material.service.MaterialService;
import com.sqx.modules.member.dao.MemberDao;
import com.sqx.modules.member.entity.Member;
import com.sqx.modules.member.service.MemberService;
import com.sqx.modules.shopping.dao.GoodsJpaRepository;
import com.sqx.modules.shopping.entity.SelfGoods;
import com.sqx.modules.shopping.service.GoodsService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private MaterialArtificerMapper materialArtificerMapper;
    @Autowired
    private MaterialMassageMapper materialMassageMapper;
    @Autowired
    private GoodsService goodsService;
    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    public List<Material> listAll() {
        return materialMapper.listAll();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    public Material getById(Integer id) {
        return materialMapper.getById(id);
    }

    /**
     * 新增，插入所有字段
     *
     * @param material 新增的记录
     * @return 返回影响行数
     */
    public int insert(Material material) {
        return materialMapper.insert(material);
    }

    /**
     * 新增，忽略null字段
     *
     * @param material 新增的记录
     * @return 返回影响行数
     */
    public Result insertIgnoreNull(Material material) {
        material.setCreateTime(new Date());
        material.setUpdateTime(new Date());
//        // 创建服务查询封装器
//        QueryWrapper<Material> materialQueryWrapper = new QueryWrapper();
//        // 设置查询条件，根据物料ID查询物料按摩信息
//        materialQueryWrapper.eq("goods_id", material.getGoodsId());
//        // 根据查询封装器查询物料按摩信息
//        Material materials =  baseMapper.selectOne(materialQueryWrapper);
//        if(materials!=null && !materials.getId().equals(material.getId()) ){
//            return Result.error("当前商品已绑定物料包，请重新选择！");
//        }
        return Result.success().put("data",materialMapper.insertIgnoreNull(material));
    }

    /**
     * 修改，修改所有字段
     *
     * @param material 修改的记录
     * @return 返回影响行数
     */
    public int update(Material material) {
        return materialMapper.update(material);
    }

    /**
     * 修改，忽略null字段
     *
     * @param material 修改的记录
     * @return 返回影响行数
     */
    public Result updateIgnoreNull(Material material) {
        material.setUpdateTime(new Date());
//        QueryWrapper<Material> materialQueryWrapper = new QueryWrapper();
//        // 设置查询条件，根据物料ID查询物料按摩信息
//        materialQueryWrapper.eq("goods_id", material.getGoodsId());
//        // 根据查询封装器查询物料按摩信息
//        Material materials =  baseMapper.selectOne(materialQueryWrapper);
//        if(ObjectUtil.isNotEmpty(materials) && !materials.getId().equals(material.getId()) ){
//            return Result.error("当前商品已绑定物料包，请重新选择！");
//        }
        return Result.success().put("data",materialMapper.updateIgnoreNull(material));
    }

    /**
     * 删除记录
     *
     * @param material 待删除的记录
     * @return 返回影响行数
     */
    public Result delete(Material material) {
        //查询该物料包是否绑定技师或者服务如果绑定了返回错误禁止删除
        // 创建服务查询封装器
        QueryWrapper<MaterialMassage> materialMassageQueryWrapper = new QueryWrapper();
        // 设置查询条件，根据物料ID查询物料按摩信息
        materialMassageQueryWrapper.eq("material_id", material.getId());
        // 根据查询封装器查询物料按摩信息
        List<MaterialMassage> materialMassages =  materialMassageMapper.selectList(materialMassageQueryWrapper);
        // 创建技师查询封装器
        QueryWrapper<MaterialArtificer> materialArtificerQueryWrapper = new QueryWrapper();
        // 设置查询条件，根据物料ID查询技师信息
        materialArtificerQueryWrapper.eq("material_id", material.getId());
        // 根据查询封装器查询物料技师信息
        List<MaterialArtificer> materialArtificers =  materialArtificerMapper.selectList(materialArtificerQueryWrapper);
        // 如果物料按摩信息或技师信息存在，则返回错误信息
        if (materialMassages.size() > 0 || materialArtificers.size() > 0){
            return Result.error("该物料包已被技师或服务绑定，无法删除");
        }
        return Result.success().put("data", materialMapper.delete(material));
    }

    @Override
    /**
     * 查询分页数据
     * @param page 当前页码
     * @param limit 每页显示数量
     * @param material 物料对象
     * @return 结果对象
     */
    public Result selectPage(Integer page, Integer limit, Material material) {
        IPage<Material> page1 = new Page(page, limit);
        IPage<Material> materialIPage = materialMapper.selectPages(page1,material);
        return Result.success().put("data", materialIPage);
    }


    @Override
    public Result selectMaterialArtificer(Integer page, Integer limit, MaterialArtificer materialArtificer) {
        if(ObjectUtil.isEmpty(page) || ObjectUtil.isEmpty(limit)){
            List<MaterialArtificerResult> materialIPage = materialMapper.selectMaterialArtificerList(materialArtificer);
            for(MaterialArtificerResult par:materialIPage){
                if(par.getResidue().compareTo(BigDecimal.ZERO)<0){
                    par.setResidue(BigDecimal.ZERO);
                }
            }
            return Result.success().put("data", materialIPage);
        }else {
            IPage<MaterialArtificerResult> page1 = new Page(page, limit);
            IPage<MaterialArtificerResult> materialIPage = materialMapper.selectMaterialArtificer(page1,materialArtificer);
            for(MaterialArtificerResult par:materialIPage.getRecords()){
                if(par.getResidue().compareTo(BigDecimal.ZERO)<0){
                    par.setResidue(BigDecimal.ZERO);
                }
            }
            return Result.success().put("data", materialIPage);
        }
    }

    @Override
    public Result selectMaterialMassage(Integer page, Integer limit, MaterialMassage materialMassage) {
        IPage<MaterialMassageResult> page1 = new Page(page, limit);
        IPage<MaterialMassageResult> materialIPage = materialMapper.selectMaterialMassage(page1,materialMassage);
        return Result.success().put("data", materialIPage);
    }

    @Override
    public Result materialList(Material material) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper();
        if(ObjectUtil.isNotEmpty(material.getStatus())) {
            queryWrapper.eq("status", material.getStatus());
        }
        if(StringUtil.isNotEmpty(material.getMaterialName())) {
            queryWrapper.eq("material_name", material.getMaterialName());
        }
        if(ObjectUtil.isNotEmpty(material.getGoodsId())) {
            queryWrapper.eq("goods_id", material.getGoodsId());
        }
        //筛选数据为未被删除
        queryWrapper.eq("is_deleted", "0");
        queryWrapper.orderByDesc(   "update_time");
        List<Material> materialIPage = materialMapper.selectList(queryWrapper);
        return Result.success().put("data", materialIPage);
    }


}
