package com.jeeplus.psimanage.dishManage.service;

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.jeeplus.common.TokenProvider;
import com.jeeplus.psimanage.dishManage.domain.PsiDishLibrary;
import com.jeeplus.psimanage.dishManage.domain.PsiDishType;
import com.jeeplus.psimanage.dishManage.mapper.PsiDishLibraryMapper;
import com.jeeplus.psimanage.dishManage.mapper.PsiDishTypeMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

/**
 * 菜品库
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PsiDishLibraryService extends ServiceImpl<PsiDishLibraryMapper, PsiDishLibrary> {

    @Resource
    private PsiDishLibraryMapper mapper;

    @Resource
    private PsiDishTypeMapper dishTypeMapper;

    public IPage<PsiDishLibrary> list(Page<PsiDishLibrary> page, PsiDishLibrary info) {
        QueryWrapper<PsiDishLibrary> wrapper = new QueryWrapper<>();
        wrapper.eq("a.del_flag", "0");
        if (StringUtils.isNotBlank(info.getDishName())) {
            wrapper.like("a.dish_name", info.getDishName());
        }
        if (StringUtils.isNotBlank(info.getDishCode())) {
            wrapper.like("a.dish_code", info.getDishCode());
        }
        if (StringUtils.isNotBlank(info.getTypeId())) {
            wrapper.eq("a.type_id", info.getTypeId());
        }
        if (StringUtils.isNotBlank(info.getStatus())) {
            wrapper.eq("a.status", info.getStatus());
        }
        return mapper.findList(page, wrapper);
    }

    public PsiDishLibrary findById(String id) {
        return mapper.queryById(id);
    }

    public String saveDish(PsiDishLibrary info) {
        if (StringUtils.isNotBlank(info.getTypeId())) {
            PsiDishType dishType = dishTypeMapper.selectById(info.getTypeId());
            if (dishType == null || !"0".equals(dishType.getStatus())) {
                return "请选择启用状态的菜品分类";
            }
            Integer childCount = dishTypeMapper.countChildren(info.getTypeId());
            if (childCount != null && childCount > 0) {
                return "菜品分类只能选择最子级分类";
            }
        }
        if (StringUtils.isNotBlank(info.getDishCode())) {
            Integer codeCount = mapper.checkCodeIsExist(info.getDishCode(), info.getId());
            if (codeCount != null && codeCount > 0) {
                return "菜品编码重复，请重新填写";
            }
        } else {
            info.setDishCode(genDishCode());
        }
        Integer nameCount = mapper.checkNameIsExist(info.getDishName(), info.getTypeId(), info.getId());
        if (nameCount != null && nameCount > 0) {
            return "同一分类下菜品名称重复，请重新填写";
        }
        UserDTO userDto = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        if (StringUtils.isNotBlank(info.getId())) {
            info.setUpdateById(userDto.getId());
            info.setUpdateTime(new Date());
            mapper.updateById(info);
        } else {
            info.setId(UUID.randomUUID().toString().replace("-", ""));
            info.setCreateById(userDto.getId());
            info.setCreateTime(new Date());
            info.setUpdateById(userDto.getId());
            info.setUpdateTime(new Date());
            info.setDelFlag(0);
            if (StringUtils.isBlank(info.getStatus())) {
                info.setStatus("0");
            }
            mapper.insert(info);
        }
        return "操作完成";
    }

    public String deleteByIds(String ids) {
        if (StringUtils.isBlank(ids)) {
            return "请选择要删除的数据";
        }
        mapper.deleteBatchIds(Arrays.asList(ids.split(",")));
        return "删除成功";
    }

    public String changeStatus(String id, String status) {
        PsiDishLibrary info = new PsiDishLibrary();
        info.setId(id);
        info.setStatus(status);
        mapper.updateById(info);
        return "操作成功";
    }

    private String genDishCode() {
        return "CP" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }
}
