package com.tzy.controller.business;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;

import com.tzy.annotation.ApiLog;
import com.tzy.business.domain.AppUserBusinessSettled;
import com.tzy.business.service.IAppUserBusinessSettledService;
import com.tzy.common.dto.InDto;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.*;
import com.tzy.common.enums.BusinessType;
import com.tzy.common.core.controller.BaseController;
import com.tzy.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 商家入驻Controller
 * 
 * @author poyee
 * @date 2023-03-09
 */
@Api("商家入驻")
@Slf4j
@RestController
@RequestMapping("/api/settled")
public class AppUserBusinessSettledController extends BaseController {

    @Resource
    private IAppUserBusinessSettledService appUserBusinessSettledService;


    @ApiOperation("查询商家入驻列表")
    @ApiLog(title = "查询商家入驻列表", businessType = BusinessType.SEARCH)
    @PostMapping("/list")
    public TableDataInfo list(AppUserBusinessSettled appUserBusinessSettled) {
        startPage();
        List<AppUserBusinessSettled> list = appUserBusinessSettledService.selectAppUserBusinessSettledList(appUserBusinessSettled);
        return getDataTable(list);
    }

    @ApiOperation("新增商家入驻")
    @ApiLog(title = "新增商家入驻", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public OutDTO addSave(@RequestBody InDto inDto) {
        AppUserBusinessSettled appUserBusinessSettled = new AppUserBusinessSettled();
        try {
            DateConverter dateConverter = new DateConverter(null);
            String[] datepattern = {DateUtils.YYYY_MM_DD, DateUtils.YYYY_MM_DD_HH_MM_SS };
            dateConverter.setPatterns(datepattern);
            ConvertUtils.register(dateConverter, java.util.Date.class);
            ConvertUtils.register(new BigDecimalConverter(0.0), BigDecimal.class);
            BeanUtils.populate(appUserBusinessSettled, inDto.getData());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        appUserBusinessSettledService.insertAppUserBusinessSettled(appUserBusinessSettled);
        return OutDTO.ok();
    }

    @ApiOperation("查询商家入驻详情")
    @ApiLog(title = "查询商家入驻详情", businessType = BusinessType.SEARCH)
    @PostMapping("/findById")
    public OutDTO findById(@RequestBody InDto inDto) {
        List<AppUserBusinessSettled> list = appUserBusinessSettledService.selectAppUserBusinessSettledById(inDto.getUserId());
        return OutDTO.ok().put("info", list);
    }

    @ApiOperation("修改商家入驻")
    @ApiLog(title = "修改商家入驻", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public OutDTO editSave(@RequestBody InDto inDto) {
        AppUserBusinessSettled appUserBusinessSettled = new AppUserBusinessSettled();
        try {
            DateConverter dateConverter = new DateConverter(null);
            String[] datepattern = {DateUtils.YYYY_MM_DD, DateUtils.YYYY_MM_DD_HH_MM_SS };
            dateConverter.setPatterns(datepattern);
            ConvertUtils.register(dateConverter, java.util.Date.class);
            ConvertUtils.register(new BigDecimalConverter(0.0), BigDecimal.class);
            BeanUtils.populate(appUserBusinessSettled, inDto.getData());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        appUserBusinessSettledService.updateAppUserBusinessSettled(appUserBusinessSettled);
        return OutDTO.ok();
    }

    @ApiOperation("删除商家入驻")
    @ApiLog(title = "删除商家入驻", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public OutDTO delete(@RequestBody InDto inDto) {
        String idStr = inDto.getData().get("id").toString();
        appUserBusinessSettledService.deleteAppUserBusinessSettledById(Long.valueOf(idStr));
        return OutDTO.ok();
    }

}
