package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxBrand;
import com.iformall.domain.po.WxShop;
import com.iformall.enums.EnumDelFlag;
import com.iformall.exception.MallinkException;
import com.iformall.mapper.WxBrandMapper;
import com.iformall.service.WxBrandService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * @author gongbiao
 */
@Service
public class WxBrandServiceImpl implements WxBrandService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxBrandMapper wxBrandMapper;

    @Override
    public PageInfo<WxBrand> listAsPage(WxBrand record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxBrandMapper.findList(record));
    }

    @Override
    public WxBrand getById(Long id) {
        return wxBrandMapper.selectById(id);
    }

    @Override
    public Collection<WxBrand> getByIds(Collection<? extends Serializable> ids) {
        if(CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>() ;
        }
        return wxBrandMapper.selectBatchIds(ids);
    }

    @Override
    public void deleteById(Long id) {
        wxBrandMapper.deleteById(id);
    }

    @Override
    public ResultData save(WxBrand wxBrand) {
        if(StringUtils.isEmpty(wxBrand.getName())){
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL,"品牌名称不能为空");
        }
        try {
            final IdWorker idWorker = IdWorker.get();
            long id = idWorker.nextId();
            wxBrand.setId(id);
            Date date = new Date();
            wxBrand.setCreatetime(date);
            wxBrand.setUpdatetime(date);
            wxBrandMapper.insert(wxBrand);
            return new ResultData(Result.SUCCESS, "操作成功", id);
        } catch (Exception e) {
            logger.error("保存租赁合同信息失败,e:" + e.getMessage());
            throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "DB FAILD " + e.getMessage());
        }
    }

    @Override
    public ResultData update(WxBrand wxBrand) {
        if(EnumDelFlag.YES.getCode().equals(wxBrand.getIsDel())){
            wxBrand.setUpdatetime(new Date());
            wxBrandMapper.updateById(wxBrand);
            return new ResultData(Result.SUCCESS, "操作成功");
        }

        if(StringUtils.isEmpty(wxBrand.getName())){
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL,"品牌名称不能为空");
        }
        try {
            wxBrand.setUpdatetime(new Date());
            wxBrandMapper.updateById(wxBrand);
            return new ResultData(Result.SUCCESS, "操作成功");
        } catch (Exception e) {
            logger.error("更新租赁合同信息失败,e:" + e.getMessage());
            throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "DB FAILD " + e.getMessage());
        }
    }

    @Override
    public List<Map<String,Object>> queryBrand(String tenantId) {
        return wxBrandMapper.queryBrand(tenantId);
    }

    @Override
    public ResultData hasBrand(String tenantId, String name, Long id) {
        WxBrand wxBrand = new WxBrand();
        wxBrand.setTenantId(tenantId);
        wxBrand.setName(name);
        wxBrand.setId(id);
        int count = wxBrandMapper.hasBrand(wxBrand);
        return new ResultData(ResultData.SUCCESS, "查询成功", count > 0 ? true : false);
    }


}
