package thyyxxk.webserver.service.lc.company;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.lc.company.LcCompanyDao;
import thyyxxk.webserver.entity.lc.company.LcCompany;
import thyyxxk.webserver.entity.lc.company.LcCompanyParam;
import thyyxxk.webserver.entity.lc.purchasePlan.LcPurchasePlanDetail;

import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class LcCompanyService {

    @Autowired
    private LcCompanyDao dao;

    public LcCompany getLcCompanyById(String id) {
        return dao.selectById(id);
    }

    public Object getLcCompanyList(LcCompanyParam lcCompanyParam) {
        LambdaQueryWrapper<LcCompany> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(lcCompanyParam.getCompanyName())) {
            queryWrapper.like(LcCompany::getCompanyName, lcCompanyParam.getCompanyName());
        }

        Integer pageNumber = lcCompanyParam.getPageNumber();
        Integer pageSize = lcCompanyParam.getPageSize();
        if (pageNumber != null && pageSize != null) {
            Page<LcCompany> page = new Page<>(pageNumber, pageSize);
            return dao.selectPage(page, queryWrapper);
        } else {
            return dao.selectList(queryWrapper);
        }
    }

    public int insertLcCompany(LcCompany lcCompany) {
        int num = 0;
        if(StringUtils.isNotBlank(lcCompany.getId())) {
            num = dao.updateById(lcCompany);
        } else {
            lcCompany.setId(UUID.randomUUID().toString().replace("-", ""));
            String serialNo = dao.getSerialNo(lcCompany.getCompanyType());
            lcCompany.setSerialNo(serialNo);
            String companyCode = "";
            if("0".equals(lcCompany.getCompanyType())) {
                companyCode = "XS0";
            } else {
                companyCode = "SC1";
            }
            companyCode += serialNo;
            lcCompany.setCompanyCode(companyCode);
            num = dao.insert(lcCompany);

        }

        return num;
    }
}
