package com.jeeplus.test.help.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.google.common.collect.Lists;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceDTO;
import com.jeeplus.test.help.domain.Help;
import com.jeeplus.test.help.mapper.HelpMapper;
import com.jeeplus.test.help.mapper.HelpTreeMapper;
import com.jeeplus.test.help.service.dto.HelpDTO;
import com.jeeplus.test.help.service.mapstruct.HelpWrapper;
import com.jeeplus.test.mould.service.SerialnumTplService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = Exception.class)
public class HelpService extends ServiceImpl<HelpMapper, Help> {

    @Resource
    private HelpMapper helpMapper;

    @Resource
    private SerialnumTplService serialnumTplService;

    @Resource
    private HelpTreeMapper helpTreeMapper;

    public IPage<HelpDTO> findList(Page<HelpDTO> page, HelpDTO helpDTO){
        QueryWrapper<Help> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("h.del_flag","0");
        if (ObjectUtil.isNotEmpty(helpDTO)) {
            if (StringUtils.isNotBlank(helpDTO.getDescription())) {
                queryWrapper.like("h.description",helpDTO.getDescription());
            }
            if (StringUtils.isNotBlank(helpDTO.getFeatures())) {
                queryWrapper.like("h.features",helpDTO.getFeatures());
            }
            if (StringUtils.isNotBlank(helpDTO.getNumber())) {
                queryWrapper.like("h.number",helpDTO.getNumber());
            }
            if (StringUtils.isNotBlank(helpDTO.getTreeId())) {
                queryWrapper.eq("h.tree_id",helpDTO.getTreeId());
            }
        }
        queryWrapper.orderByDesc("h.create_date");
        return helpMapper.findList(page,queryWrapper);
    }

    public HelpDTO queryById(String id) {

        HelpDTO helpDTO = helpMapper.queryById(id);

        return helpDTO;
    }

    public ResponseEntity saveHelp(HelpDTO helpDTO) throws Exception{
        if (ObjectUtil.isNotEmpty(helpDTO) && StringUtils.isBlank(helpDTO.getId())) {
            String serialNum = serialnumTplService.genSerialNum(UserUtils.getCurrentUserDTO().getCompanyDTO().getId(), HelpDTO.BIZ_CODE);
            helpDTO.setNumber(serialNum);
        }
        Help help = HelpWrapper.INSTANCE.toEntity(helpDTO);
        this.saveOrUpdate(help);
        return ResponseEntity.ok("保存成功");
    }

    public ResponseEntity deleteByIds(String ids) {
        String idArray[] =ids.split(",");
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }

    // 帮助管理状态启用/停用
    public ResponseEntity updateStatus (HelpDTO helpDTO) {
        if (ObjectUtil.isNotEmpty(helpDTO) && StringUtils.isNotBlank(helpDTO.getId())) {
            // 查询此数据的状态
            Help help = getById(helpDTO.getId());
            if (ObjectUtil.isNotEmpty(help) && StringUtils.isNotBlank(help.getUseFlag())) {
                if ("1".equals(help.getUseFlag())) { // 当前状态为启用
                    help.setUseFlag("0"); // 设置为停用
                    updateById(help);
                    return ResponseEntity.ok("修改成功，”"+help.getNumber()+"“调整为停用");
                } else if ("0".equals(help.getUseFlag())) { // 当前状态为停用
                    help.setUseFlag("1"); // 设置为启用
                    updateById(help);
                    return ResponseEntity.ok("修改成功，”"+help.getNumber()+"“调整为启用");
                }
            }
        }
        return ResponseEntity.badRequest().body("修改失败");
    }


}
