#删除产品数据
DELETE FROM pms_product WHERE create_time >='2020-10-9 00:00:00';

#供应商
DELETE FROM ums_company_info WHERE create_time >='2020-10-9 00:00:00';
#供应商文件
DELETE FROM ums_company_file WHERE create_time >='2020-10-9 00:00:00';
#供应商商店
DELETE FROM ums_company_shop WHERE create_time >='2020-10-9 00:00:00';

#生产地
DELETE FROM ums_member_origin WHERE create_time >='2020-10-9 00:00:00';
#生产地产出物
DELETE FROM ums_member_origin_product WHERE create_time >='2020-10-9 00:00:00';

# 更新商品大类
UPDATE pms_product a
LEFT JOIN pms_product_category b ON a.product_big_category_name = b.`name`
SET a.product_big_category_id = b.id
WHERE
	b.parent_id = 0
AND a.create_time >= '2020-10-9 00:00:00';

# 更新商品小类
UPDATE pms_product a
LEFT JOIN pms_product_category c ON a.product_category_name = c.`name`
SET a.product_category_id = c.id
WHERE
	c.parent_id != 0
AND a.create_time >= '2020-10-9 00:00:00';

# 更新商品大类小类
UPDATE pms_product a
LEFT JOIN pms_product_category b ON a.product_big_category_name = b.`name`
LEFT JOIN pms_product_category c ON a.product_category_name = c.`name`

SET a.product_big_category_id = b.id,
 a.product_category_id = c.id
WHERE
	b.parent_id = 0
AND c.parent_id != 0
AND a.create_time >= '2020-10-9 00:00:00';

# 品牌表新增字段

ALTER TABLE pms_brand ADD COLUMN `create_id` BIGINT (20) DEFAULT NULL COMMENT '创建人' AFTER `brand_story`;

ALTER TABLE pms_brand ADD COLUMN `create_time` datetime DEFAULT NULL COMMENT '创建时间' AFTER `create_id`;

ALTER TABLE pms_brand ADD COLUMN `update_id` BIGINT (20) DEFAULT NULL COMMENT '更新人' AFTER `create_time`;

ALTER TABLE pms_brand ADD COLUMN `update_time` datetime DEFAULT NULL COMMENT '更新时间' AFTER `update_id`;


# 品牌表新增唯一索引
ALTER TABLE `pms_brand` ADD UNIQUE (`name`)


# 产品索引
ALTER TABLE `oms_pre_item` ADD INDEX idx_product_id ( `product_id` );

# 产品购买人id索引
ALTER TABLE `oms_pre_item` ADD INDEX idx_member_id ( `member_id` );

# 产品供应商id索引
ALTER TABLE `oms_pre_item` ADD INDEX idx_comp_id ( `comp_id` );

# 产品发布人id索引
ALTER TABLE `oms_pre_item` ADD INDEX idx_push_id ( `push_id` );

# 生产地id索引
ALTER TABLE `oms_pre_item` ADD INDEX idx_origin_id ( `origin_id` );


# 产品品牌id索引
ALTER TABLE `pms_product` ADD INDEX idx_brand_id ( `brand_id` );

# 产品产品大类标识索引
ALTER TABLE `pms_product` ADD INDEX idx_product_big_category_id ( `product_big_category_id` );

# 产品产品类别标识索引
ALTER TABLE `pms_product` ADD INDEX idx_product_category_id ( `product_category_id` );

# 产品生产地id索引
ALTER TABLE `pms_product` ADD INDEX idx_place_of_production_id( `place_of_production_id` );

# 删除重复供应商
SELECT DISTINCT
	aa.comp_id
FROM
	pms_product aa;

DELETE
FROM
	ums_company_info
WHERE
	id NOT IN (

	)
## 清楚多余生产地
#供应商
DELETE FROM ums_company_info WHERE id = 298;

#删除产品数据
DELETE FROM pms_product WHERE comp_id = 298;

#供应商文件
DELETE FROM ums_company_file WHERE comp_id = 298;
#供应商商店
DELETE FROM ums_company_shop WHERE comp_id = 298;

#生产地产出物
DELETE FROM ums_member_origin_product WHERE fk_origin_id in (select a.id from ums_member_origin a where a.comp_id = 298);

#生产地
DELETE FROM ums_member_origin WHERE comp_id = 298;

#意向表
DELETE FROM oms_pre_item WHERE comp_id = 298;


# 清楚多余文件
SELECT DISTINCT
	a.comp_id
FROM
	pms_product a
WHERE
	a.delete_status = 0;

#供应商文件
DELETE
FROM
	ums_company_file
WHERE
	comp_id NOT IN (
		SELECT DISTINCT
			a.comp_id
		FROM
			pms_product a
		WHERE
			a.delete_status = 0
	);

#供应商商店
DELETE
FROM
	ums_company_shop
WHERE
	comp_id NOT IN (
		SELECT DISTINCT
			a.comp_id
		FROM
			pms_product a
		WHERE
			a.delete_status = 0
	);

#生产地产出物
DELETE
FROM
	ums_member_origin_product
WHERE
	fk_origin_id IN (
		SELECT
			a.id
		FROM
			ums_member_origin a
		WHERE
			a.comp_id NOT IN (
				SELECT DISTINCT
					a.comp_id
				FROM
					pms_product a
				WHERE
					a.delete_status = 0
			)
	);

#生产地
DELETE
FROM
	ums_member_origin
WHERE
	comp_id NOT IN (
		SELECT DISTINCT
			a.comp_id
		FROM
			pms_product a
		WHERE
			a.delete_status = 0
	);


