package com.neusoft.smart.pos.ota.persistent.repository;

import com.neusoft.smart.pos.ota.persistent.domain.UpgradePackage;
import com.neusoft.smart.pos.ota.persistent.domain.UpgradePackageExample;
import com.neusoft.smart.pos.ota.persistent.ext.domain.UpgradePackageQuery;
import com.neusoft.smart.pos.ota.persistent.mapper.UpgradePackageMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

@Component
public class UpgradePackageRepository {
    
    @Autowired
    private UpgradePackageMapper upgradePackageMapper;
    
    /**
     * @param upgradePackage
     * @return
     */
    public UpgradePackage create(UpgradePackage upgradePackage) {
        upgradePackageMapper.insertSelective(upgradePackage);
        return upgradePackage;
    }
    
    /**
     * @param packageId
     */
    public void delete(Integer packageId) {
        UpgradePackage upgradePackage = new UpgradePackage();
        upgradePackage.setStatus(1);
        upgradePackage.setId(packageId);
        upgradePackageMapper.updateByPrimaryKeySelective(upgradePackage);
    }
    
    /**
     * @param upgradePackageQuery
     * @return
     */
    public List<UpgradePackage> queryList(UpgradePackageQuery upgradePackageQuery) {
        UpgradePackageExample upgradePackageExample = this.exampleProvider(upgradePackageQuery);
        if (upgradePackageQuery.getIsPage()) {
            upgradePackageExample.setLimit(upgradePackageQuery.getLimit());
            upgradePackageExample.setOffset(upgradePackageQuery.getOffset());
        }
        return upgradePackageMapper.selectByExample(upgradePackageExample);
    }
    
    /**
     * @param upgradePackageQuery
     * @return
     */
    public Integer queryCount(UpgradePackageQuery upgradePackageQuery) {
        UpgradePackageExample upgradePackageExample = this.exampleProvider(upgradePackageQuery);
        return upgradePackageMapper.countByExample(upgradePackageExample);
    }
    
    /**
     * @param packageId
     * @return
     */
    public UpgradePackage get(Integer packageId) {
        return upgradePackageMapper.selectByPrimaryKey(packageId);
    }
    
    private UpgradePackageExample exampleProvider(@NotNull UpgradePackageQuery upgradePackageQuery) {
        UpgradePackageExample upgradePackageExample = new UpgradePackageExample();
        upgradePackageExample.setOrderByClause("created_date desc");
        UpgradePackageExample.Criteria criteria = upgradePackageExample.createCriteria();
        if (!StringUtils.isEmpty(upgradePackageQuery.getVersionSuffix())) {
            criteria.andVersionCodeLike("." + upgradePackageQuery.getVersionSuffix() + "%");
        }
        if (!StringUtils.isEmpty(upgradePackageQuery.getCustomer())) {
            criteria.andCustomerLike("%" + upgradePackageQuery.getCustomer() + "%");
        }
        if (!StringUtils.isEmpty(upgradePackageQuery.getDeviceModel())) {
            criteria.andDeviceModelLike("%" + upgradePackageQuery.getDeviceModel() + "%");
        }
        if (!StringUtils.isEmpty(upgradePackageQuery.getDeviceType())) {
            criteria.andDeviceTypeLike("%" + upgradePackageQuery.getDeviceType() + "%");
        }
        if (!StringUtils.isEmpty(upgradePackageQuery.getManufacturer())) {
            criteria.andManufacturerLike("%" + upgradePackageQuery.getManufacturer() + "%");
        }
        if (!StringUtils.isEmpty(upgradePackageQuery.getVersionCode())) {
            criteria.andVersionCodeLike("%" + upgradePackageQuery.getVersionCode() + "%");
        }
        
        if (!StringUtils.isEmpty(upgradePackageQuery.getStartDate())) {
            try {
                criteria.andCreatedDateGreaterThanOrEqualTo(DateUtils.parseDate(upgradePackageQuery.getStartDate() + " 00:00:00", new String[]{"yyyy-MM-dd HH:mm:ss"}));
            } catch (ParseException ignored) {
            }
        }
        if (!StringUtils.isEmpty(upgradePackageQuery.getEndDate())) {
            try {
                criteria.andCreatedDateLessThanOrEqualTo(DateUtils.parseDate(upgradePackageQuery.getEndDate() + " 23:59:59", new String[]{"yyyy-MM-dd HH:mm:ss"}));
            } catch (ParseException ignored) {
            }
        }
        if (null != upgradePackageQuery.getFileTypes() && !upgradePackageQuery.getFileTypes().isEmpty()) {
            criteria.andFileTypeIn(upgradePackageQuery.getFileTypes());
        }
        if (null != upgradePackageQuery.getOrganizationId()) {
            criteria.andOrganizationIdEqualTo(upgradePackageQuery.getOrganizationId());
        }
        if (null != upgradePackageQuery.getStatusArr()) {
            criteria.andStatusIn(Arrays.asList(upgradePackageQuery.getStatusArr()));
        } else {
            criteria.andStatusNotEqualTo(1);//过滤删除的
        }
        if (null != upgradePackageQuery.getIds() && !upgradePackageQuery.getIds().isEmpty()) {
            criteria.andIdIn(upgradePackageQuery.getIds());
        }
        if (!StringUtils.isEmpty(RequestHeaderUtil.getRole()) && !"sa".equals(RequestHeaderUtil.getRole())) {
            criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
        }
        return upgradePackageExample;
    }
}
