package com.iotechn.unimall.app.api.market;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.market.MarketReceivingAddress;
import com.iotechn.unimall.data.mapper.market.MarketReceivingAddressMapper;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.ExcelUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 收货地址Service业务层处理
 *
 * @author jlb
 * @date 2022-08-01
 */
@Service
public class MarketReceivingAddressAppServiceImpl implements IMarketReceivingAddressAppService {
    @Autowired
    private MarketReceivingAddressMapper marketReceivingAddressMapper;

    @Override
    public Boolean add(MarketReceivingAddress marketReceivingAddress, Long adminId) throws ServiceException {
        Date now = new Date();
        marketReceivingAddress.setGmtCreate(now);
        marketReceivingAddress.setGmtUpdate(now);
        marketReceivingAddress.setAdminId(adminId);
        return marketReceivingAddressMapper.insert(marketReceivingAddress) > 0;
    }

    @Override
    public Page<MarketReceivingAddress> list(Long companyId, String cusId, String consigneeName, String phone, String location, String address, String status, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<MarketReceivingAddress> wrapper = new EntityWrapper<MarketReceivingAddress>();
        if (!StringUtils.isEmpty(companyId)) {
            wrapper.eq("company_id", companyId);
        }
        if (!StringUtils.isEmpty(cusId)) {
            wrapper.eq("cus_id", cusId);
        }
        if (!StringUtils.isEmpty(consigneeName)) {
            wrapper.eq("consignee_name", consigneeName);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(location)) {
            wrapper.eq("location", location);
        }
        if (!StringUtils.isEmpty(address)) {
            wrapper.eq("address", address);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id", userId);
        }
        if (!StringUtils.isEmpty(adminId)) {
            wrapper.eq("admin_id", adminId);
        }
        wrapper.eq("delete_flag", 0);
        List<MarketReceivingAddress> list = marketReceivingAddressMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = marketReceivingAddressMapper.selectCount(wrapper);
        return new Page<MarketReceivingAddress>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id) {
        String[] ids = String.valueOf(id).split(",");
        for (String tt : ids) {
            MarketReceivingAddress tmp = marketReceivingAddressMapper.selectById(Long.parseLong(tt));
            if (tmp != null) {
                tmp.setDeleteFlag(1);
                marketReceivingAddressMapper.updateById(tmp);
            }
        }
        return true;
    }

    @Override
    public Boolean update(MarketReceivingAddress marketReceivingAddress, Long adminId) throws ServiceException {
        Date now = new Date();
        marketReceivingAddress.setGmtUpdate(now);
        marketReceivingAddress.setAdminId(adminId);
        return marketReceivingAddressMapper.updateById(marketReceivingAddress) > 0;
    }

    @Override
    public MarketReceivingAddress get(Long id) throws ServiceException {
        return marketReceivingAddressMapper.selectById(id);
    }

    @Override
    public String export(Long companyId, String cusId, String consigneeName, String phone, String location, String address, String status, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<MarketReceivingAddress> wrapper = new EntityWrapper<MarketReceivingAddress>();
        if (!StringUtils.isEmpty(companyId)) {
            wrapper.eq("company_id", companyId);
        }
        if (!StringUtils.isEmpty(cusId)) {
            wrapper.eq("cus_id", cusId);
        }
        if (!StringUtils.isEmpty(consigneeName)) {
            wrapper.eq("consignee_name", consigneeName);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(location)) {
            wrapper.eq("location", location);
        }
        if (!StringUtils.isEmpty(address)) {
            wrapper.eq("address", address);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id", userId);
        }
        if (!StringUtils.isEmpty(adminId)) {
            wrapper.eq("admin_id", adminId);
        }
        List<MarketReceivingAddress> list = marketReceivingAddressMapper.selectList(wrapper);
        ExcelUtil<MarketReceivingAddress> util = new ExcelUtil<MarketReceivingAddress>(MarketReceivingAddress.class);
        return util.exportExcel(list, "操作日志");
    }
}
