package org.springblade.customer.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.customer.dto.DimVipDTO;
import org.springblade.customer.entity.DimVipid;
import org.springblade.customer.query.DimVipQuery;
import org.springblade.customer.service.IDimVipidService;
import org.springblade.system.client.UserClient;
import org.springblade.system.client.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 会员池渠道字典表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2022-02-09
 */
@RestController
@RequestMapping("/dimVipid")
@AllArgsConstructor
@Api(value = "会员池渠道管理", tags = "会员池渠道接口")
@Slf4j
public class DimVipidController {


	@Autowired
	private IDimVipidService dimVipidService;

	@Autowired
	private UserClient userService;


	@GetMapping("/page")
	@ApiOperation(value = "分页")
	public R<IPage<DimVipid>> page(DimVipQuery pageQuery, Query query) {
		IPage<DimVipid> page = Condition.getPage(query);
		IPage<DimVipid> pages = dimVipidService.getPageByQuery(page, pageQuery);
		return R.data(pages);
	}


	@GetMapping("/select/qytypeList")
	@ApiOperation(value = "权益类型下拉列表")
	public R qytypeList() {
		QueryWrapper<DimVipid> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("DISTINCT QYTYPE").isNotNull("QYTYPE");
		List<DimVipid> list = dimVipidService.list(queryWrapper);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("name", obj.getQytype());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}



	@GetMapping("/select/ctypeList")
	@ApiOperation(value = "渠道分类下拉列表")
	public R ctypeList() {
		QueryWrapper<DimVipid> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("DISTINCT CTYPE").isNotNull("CTYPE");
		List<DimVipid> list = dimVipidService.list(queryWrapper);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("name", obj.getCtype());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}


	@GetMapping("/select/companyList")
	@ApiOperation(value = "公司名下拉列表")
	public R companyList() {
		QueryWrapper<DimVipid> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("DISTINCT COMPANY").isNotNull("COMPANY");
		List<DimVipid> list = dimVipidService.list(queryWrapper);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("name", obj.getCompany());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}



	@PostMapping("/editPrice")
	@ApiOperation(value = "价格编辑")
	public R companyList(@RequestBody DimVipDTO dto) {
		log.info(" dto === {}",dto.toString());
		if(dto.getId() == null){
			return R.fail("参数异常");
		}
		Long userId = SecureUtil.getUserId();
		UserModel user = userService.getById(userId);
		DimVipid vip = dimVipidService.getById(dto.getId());

		//成本价格编辑记录参数
		Double oldCostPrice = vip.getCostprice();

		//销售价格编辑记录参数
		Double oldSalePrice = vip.getSaleprice();

		if(Double.doubleToLongBits(dto.getCostprice()) != Double.doubleToLongBits(oldCostPrice)){
			vip.setCostprice(dto.getCostprice());
			vip.setCostpriceOper(user.getRealName());
			vip.setCostpriceOptime(new Date());
		}
		if(Double.doubleToLongBits(dto.getSaleprice()) != Double.doubleToLongBits(oldSalePrice)){
			vip.setSaleprice(dto.getSaleprice());
			vip.setSalepriceOper(user.getRealName());
			vip.setSalepriceOptime(new Date());
		}
		boolean sa = dimVipidService.updateById(vip);
		if(sa){
			if(Double.doubleToLongBits(dto.getCostprice()) != Double.doubleToLongBits(oldCostPrice)){
				String openType = "编辑成本价格";
				dimVipidService.sysOperationLog(vip.getId(),openType,oldCostPrice,dto.getCostprice(),user.getRealName());
			}
			if(Double.doubleToLongBits(dto.getSaleprice()) != Double.doubleToLongBits(oldSalePrice)){
				String openType = "编辑销售价格";
				dimVipidService.sysOperationLog(vip.getId(),openType,oldSalePrice,dto.getSaleprice(),user.getRealName());
			}
			return R.data(sa);
		}
		return R.fail("编辑失败");
	}

}

