package com.fs.company.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyMoneyLogs;
import com.fs.company.param.CompanyMoneyLogsParam;
import com.fs.company.service.ICompanyMoneyLogsService;
import com.fs.company.vo.CompanyMoneyLogsExport1VO;
import com.fs.company.vo.CompanyMoneyLogsExport2VO;
import com.fs.company.vo.CompanyMoneyLogsExportVO;
import com.fs.company.vo.CompanyMoneyLogsVO;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.store.vo.FsStorePaymentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 企业账户记录Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/companyMoneyLogs")
public class CompanyMoneyLogsController extends BaseController
{
    @Autowired
    private ICompanyMoneyLogsService companyMoneyLogsService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询企业账户记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyMoneyLogs:list')")
    @GetMapping("/list")

    public TableDataInfo list(CompanyMoneyLogsParam companyMoneyLogs)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyMoneyLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        if(companyMoneyLogs.getLogsType()!=null){
            if(companyMoneyLogs.getLogsType()==4 || companyMoneyLogs.getLogsType()==5){
                List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsMallVOList(companyMoneyLogs);
                return getDataTable(list);
            }
            if(companyMoneyLogs.getLogsType()==8 || companyMoneyLogs.getLogsType()==9){
                List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsMallVO1List(companyMoneyLogs);
                return getDataTable(list);
            }
        }
        List<CompanyMoneyLogsVO> list = companyMoneyLogsService.selectCompanyMoneyLogsVOList(companyMoneyLogs);
        return getDataTable(list);
    }

    @GetMapping("/getCompanyTotalMoneys")
    public R getCompanyTotalMoneys(CompanyMoneyLogsParam companyMoneyLogs)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyMoneyLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        BigDecimal totalMoneys = companyMoneyLogsService.selectCompanyTotalMoneys(companyMoneyLogs);
        return R.ok().put("totalMoneys",totalMoneys);
    }



    @PreAuthorize("@ss.hasAnyPermi('company:companyMoneyLogs:export,company:companyMoneyLogs:export1,company:companyMoneyLogs:export2')")
    @Log(title = "企业账户记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyMoneyLogs companyMoneyLogs)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyMoneyLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        if(companyMoneyLogs.getType()==0){
            List<CompanyMoneyLogsExportVO> list = companyMoneyLogsService.selectCompanyMoneyLogsExportVOList(companyMoneyLogs);
            ExcelUtil<CompanyMoneyLogsExportVO> util = new ExcelUtil<CompanyMoneyLogsExportVO>(CompanyMoneyLogsExportVO.class);
            return util.exportExcel(list, "companyMoneyLogs");
        }
        else if(companyMoneyLogs.getType()==1){
            List<CompanyMoneyLogsExport1VO> list = companyMoneyLogsService.selectCompanyMoneyLogsExport1VOList(companyMoneyLogs);
            for (CompanyMoneyLogsExport1VO vo : list){
                vo.setUserPhone(ParseUtils.parsePhone(vo.getUserPhone()));
                vo.setUserAddress(ParseUtils.parseAddress(vo.getUserAddress()));
            }
            ExcelUtil<CompanyMoneyLogsExport1VO> util = new ExcelUtil<CompanyMoneyLogsExport1VO>(CompanyMoneyLogsExport1VO.class);
            return util.exportExcel(list, "companyMoneyLogs");
        }
        else if(companyMoneyLogs.getType()==2){
            List<CompanyMoneyLogsExport2VO> list = companyMoneyLogsService.selectCompanyMoneyLogsExport2VOList(companyMoneyLogs);
            ExcelUtil<CompanyMoneyLogsExport2VO> util = new ExcelUtil<CompanyMoneyLogsExport2VO>(CompanyMoneyLogsExport2VO.class);
            return util.exportExcel(list, "companyMoneyLogs");
        }
        return new AjaxResult(500,"操作失败");
    }


}
