效果 订单号:CH2020-1003

    <?php    
    /**
     * Created by PhpStorm.
     * User: Lenovo
     * Date: 22/1/2020
     * Time: 上午10:51
     */
    namespace App\Library;
    use App\Models\SalerContractCode;
    class Common
    {
         /**
          * 获取销售合同订单号
          * @return mixed
          */
         public static function getSalerContractCode(){
              $year = date('Y');
              $data = array(
                   'year'=>$year
              );
              $insert_id =  SalerContractCode::insertGetId($data);
              return "CH".$year.'-'.$insert_id;
         }
    }

数据库表结构

    SET NAMES utf8mb4;    
    SET FOREIGN_KEY_CHECKS = 0;
    -- ----------------------------
    -- Table structure for c_saler_contract_codes
    -- ----------------------------
    DROP TABLE IF EXISTS `c_saler_contract_codes`;
    CREATE TABLE `c_saler_contract_codes`  (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `year` year NOT NULL,
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 1004 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '销售合同订单id生成器' ROW_FORMAT = Dynamic;
    -- ----------------------------
    -- Records of c_saler_contract_codes
    -- ----------------------------
    INSERT INTO `c_saler_contract_codes` VALUES (1000, 2020);
    INSERT INTO `c_saler_contract_codes` VALUES (1001, 2020);
    INSERT INTO `c_saler_contract_codes` VALUES (1002, 2020);
    INSERT INTO `c_saler_contract_codes` VALUES (1003, 2020);
    SET FOREIGN_KEY_CHECKS = 1;