package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.dto.CarDto;
import com.sptg.estate.api.entity.Car;
import com.sptg.estate.api.feigns.CarFeign;
import com.sptg.estate.biz.service.CarServiceInterface;
import com.sptg.estate.biz.util.AESPlus;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class CarController implements CarFeign {

    private final CarServiceInterface carServiceInterface;
    private static String encryptKey = "ad84afc60ddae8a8";

    @Override
    @PostMapping(value = "/car/page")
    public ResultBean<Page<Car>> page(@RequestBody CarDto dto) {
        /*Wrapper<Car> wrapper = new EntityWrapper<>();
        wrapper.like(dto.getNumber() != null, "s_number",dto.getNumber(), SqlLike.DEFAULT);
        wrapper.eq(dto.getType() != null, "i_type",dto.getType());
        wrapper.eq(dto.getProperties() != null, "i_properties",dto.getProperties());
        wrapper.like(dto.getPersonName() != null, "s_person_name",dto.getPersonName(),SqlLike.DEFAULT);
        wrapper.like(dto.getPersonPhone() != null, "s_person_phone",dto.getPersonPhone(),SqlLike.DEFAULT);
        wrapper.eq(dto.getAuditStatus() != null, "i_audit_stauts",dto.getProperties());*/
        Page<Car> page = dto.getPage();
        if(page == null){
            page = new Page<>();
            page.setSize(10);
            page.setCurrent(1);
        }
        ResultBean<Page<Car>> result = this.carServiceInterface.page(dto);
        Page<Car> pageData = result.getData();
        List<Car> carList = pageData.getRecords();
        for(Car car : carList){
            if(car.getEncrypt() == 1){
                try{
                    car.setVehicleDrivingLicense1(AESPlus.decrypt(encryptKey, car.getVehicleDrivingLicense1()));
                    car.setVehicleDrivingLicense2(AESPlus.decrypt(encryptKey, car.getVehicleDrivingLicense2()));
                }catch (Exception e){

                }
            }
        }
        return result;
    }

    @Override
    @PostMapping(value = "/car/getById")
    public Car getById(@RequestBody Long id) {
        Assert.notNull(id,"cardId不能为空");
        return this.carServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/car/add")
    public ResultBean<Boolean> add(@RequestBody CarDto dto) {
        return this.carServiceInterface.add(dto);
    }

    @Override
    @PostMapping(value = "/car/addByApp")
    public ResultBean<Boolean> addByApp(@RequestBody CarDto dto) {
        return this.carServiceInterface.addByApp(dto);
    }

    @Override
    @PostMapping(value = "/car/edit")
    public ResultBean<Boolean> edit(@RequestBody CarDto dto) {
        return this.carServiceInterface.edit(dto);
    }

    @Override
    @PostMapping(value = "/car/delete")
    public ResultBean<Boolean> delete(@RequestBody CarDto dto) {
        return this.carServiceInterface.delete(dto);
    }
}
